如何让Hibernate不丢弃表格 [英] How to make Hibernate not drop tables
问题描述
我正在使用hibernate,每当我尝试添加记录时,它都会删除表并重新添加它。它从不使用现有的表并对其进行更改。
这是我的 hibernate.cfg.xml $ c $的相关部分c>:
< 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>用户< / property>
< property name =hibernate.connection.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>更新< / property>
< property name =current_session_context_class>线程< / property>
<! - 映射实体 - >
< mapping class =inputDetails.Table1/>
< mapping class =inputDetails.Table2/>
<! - 映射资源=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();
<属性名称=hibernate.hbm2ddl.auto>更新< / property>
告诉hibernate在每次创建会话工厂时更新数据库模式。
$ b ()。$ b
和
SessionFactory factory = new Configuration()。configure()。buildSessionFactory();
建立一个新的会话工厂。
在wole应用程序生命周期中,应该只构建一次 SessionFactory
一次。它应该被创建一次然后被重用。您是否阅读过 hibernate参考手册?
I am using hibernate and whenever I try to add a record it drops the table and adds it again. It never uses the existing table and make changes on that.
This is the relevant 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 data:
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>
tells hibernate to update the database schema each time the session factory is created.
And
SessionFactory factory = new Configuration().configure().buildSessionFactory();
builds a new session factory.
A SessionFactory
should be built only once during the wole application lifetime. It should be created once and then reused. Have you read the hibernate reference manual?
这篇关于如何让Hibernate不丢弃表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!