如何使Hibernate不丢弃表

Suj*_*jen 7 hibernate

我正在使用hibernate,每当我尝试添加记录时,它都会丢弃表并再次添加它.它从不使用现有表并对其进行更改.

这是我的相关部分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>
Run Code Online (Sandbox Code Playgroud)

这是我保存数据的方式:

SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();
//...
session.save(newrecord)
session.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 18

<property name="hibernate.hbm2ddl.auto">update</property>
Run Code Online (Sandbox Code Playgroud)

告诉hibernate每次创建会话工厂时都更新数据库模式.

SessionFactory factory = new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

建立一个新的会话工厂.

A SessionFactory应该在应用程序生命周期内只构建一次.它应该创建一次然后重用.您是否阅读过hibernate参考手册