[英]Opening a new session in hibernate cancels table rows
當我執行此代碼時:
SessionFactory fact=new Configuration().configure().buildSessionFactory();
Session session=fact.openSession();
Transaction tx = session.beginTransaction(); //start transaction
不管我在那之后做什么, 它都會從數據庫中的每個表中刪除每個行 ,這可能是什么?
這是休眠配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="connection.url">jdbc:db2://localhost:50000/db2</property>
<property name="connection.username">db2admin</property>
<property name="connection.password">db2admin</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<!-- <property name="dialect">org.hibernate.dialect.HSQLDialect</property> -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- proprietà per il mapping delle classi-->
<mapping resource="hibernate/Studenti.hbm.xml"/>
<mapping resource="hibernate/Cani.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射有效,添加行有效,問題在於每次我執行代碼時,它都會刪除表中的每個先前數據。
您只需要更改休眠配置文件的以下屬性即可。
<property name="hbm2ddl.auto">create</property>
而不是把這個更新:-
<property name="hbm2ddl.auto">update</property>
那肯定會解決您的問題。
您將屬性hbm2ddl.auto設置為創建。 這意味着:每次構建會話工廠時都創建數據庫模式。 只需刪除該屬性,Hibernate就不會每次都重新創建模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.