当我执行这段代码时:
SessionFactory fact=new Configuration().configure().buildSessionFactory();
Session session=fact.openSession();
Transaction tx = session.beginTransaction(); //start transaction
无论我之后做什么,它都会删除数据库中每个表中的每一行,这会是什么?
这是 hibernate 配置文件
<?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>
映射有效,添加行有效,问题是每次我执行代码时,它都会删除表中以前的所有数据。
最佳答案
您只需更改 hibernate 配置文件的以下属性。
<property name="hbm2ddl.auto">create</property>
代替这个更新:-
<property name="hbm2ddl.auto">update</property>
这肯定能解决你的问题。
关于java - 在 hibernate 中打开新 session 会取消表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645371/