How to make Hibernate not drop tables

I use hibernate, and whenever I try to add a record, it discards the table and adds it again. It never uses an existing table and makes no changes.

This is an important part of my hibernate.cfg.xml :

 <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.DerbyDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name = "current_session_context_class">thread</property> <!-- Mapping the entities --> <mapping class="inputDetails.Table1"/> <mapping class="inputDetails.Table2"/> <!--mapping resource="contact.hbm.xml"/--> </session-factory> </hibernate-configuration> 

This is how I save the data:

 SessionFactory factory = new Configuration().configure().buildSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); //... session.save(newrecord) session.getTransaction().commit(); 
+6
source share
4 answers
 <property name="hibernate.hbm2ddl.auto">update</property> 

tells hibernate to update the database schema every time a factory session is created.

AND

 SessionFactory factory = new Configuration().configure().buildSessionFactory(); 

creates a new factory session.

A SessionFactory should only be built once during the lifetime of the application for dragging. It must be created once and then reused. Have you read the hibernate reference manual?

+16
source

Are you sure it is overwritten or not fixed? I mean, are you making transactions?

Maybe try something in the lines:

 try { factory.getCurrentSession().beginTransaction(); // Do some work factory.getCurrentSession().load(...); factory.getCurrentSession().persist(...); factory.getCurrentSession().getTransaction().commit(); } catch (RuntimeException e) { factory.getCurrentSession().getTransaction().rollback(); throw e; // or display error message } 
+1
source

Hope this helps you.

 AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Employee.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).drop(false, false); 
+1
source

I had

 <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.hbm2ddl.import_files" value="META-INF/import.sql" /> 

upon first deployment. This created my desired circuit and populated it with data. The second time I switched to

 <property name="hibernate.hbm2ddl.auto" value="update" /> 

and redistributed. Obviously, the tables were dropped. I switched to create for the first deployment and stayed with update with redistribution. These time tables were not discarded, and the data remained intact.

0
source

Source: https://habr.com/ru/post/892239/


All Articles