Reputation: 187
Im using Hibernate to save a Java class to a derby database. When I run my code everything is fine the first time I run my class ,I can create and call back an instance from my database and it persists even while I turn off my server Tomcat.
When I load back up my server I found that my database tables get wiped once I return the following class, which function isto create a sessionfactory that I use to create my sessions, so i dont have multiple factories, Its called by a ServletContextListener so that its called before any of my servevlets are called and therefore maintain one Factory per run.
Does anyone know why it keeps wiping my database, is it not just a handle
public class CreateMysessionfactory { private SessionFactory factory;
public CreateMysessionfactory() {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(UPS.class);
config.configure();
factory = config.buildSessionFactory();
}
public SessionFactory getFactory() {
return factory;
}
Below is the ServletContextListener calling the above method, it might be helpfull.
public void contextInitialized(ServletContextEvent event) {
// Create the database connection.
ServletContext ctx = event.getServletContext();
// Create the database:grab my hibernate object here
CreateMysessionfactory db = new CreateMysessionfactory();
ctx.setAttribute("db", db);
ctx.setAttribute("DbSessionFactory", db.getFactory());
ItemDataAccessObject dao = new ItemDataAccessObject(db.getFactory());
Item u = new Item();
//Item u =dao.getItem(0);
dao.updateItem(u);
Upvotes: 1
Views: 1760
Reputation: 2421
all the entities are bieng deleted on startup because you have set the hibernate config the same way,
actually the property HBM2DDl has these values validate | update | create | create-drop
you might have set it as create-drop which means , delete and create the fresh schema on startup and hence the effect.
you may like to change the value to validate, which just conforms that the schema is intact
Refer the link if you need further details http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
Upvotes: 4