我正在使用休眠,每当我尝试添加记录时,它都会删除表并再次添加它。它从不使用现有的表并对其进行更改。
这是我的相关部分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>
这就是我保存数据的方式:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
//...
session.save(newrecord)
session.getTransaction().commit();
<property name="hibernate.hbm2ddl.auto">update</property>
告诉 hibernate 在每次创建会话工厂时更新数据库模式。
And
SessionFactory factory = new Configuration().configure().buildSessionFactory();
建立一个新的会话工厂。
A SessionFactory
在整个应用程序生命周期中应该只构建一次。它应该创建一次然后重复使用。你读过吗休眠参考手册 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)