我有一个从h2数据库保存和读取数据的类。如果我使用“mem”属性它工作正常。但是如果我使用“file”属性,在我停止程序后,所有数据都将从db中删除。 我的冬眠cfg:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:file:d:\WebProjectDb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.webproject.Courses"/>
</session-factory>
</hibernate-configuration>
这是我从db读取和保存的方法: SessionFactory sf = new AnnotationConfiguration()。configure()。buildSessionFactory();
public void save(Courses user) {
Session session = sf.openSession();
session.save(user);
session.flush();
session.close();
}
public List<Courses> getCourses(){
Session session = sf.openSession();
List courses = session.createQuery("from Courses").list();
session.close();
return courses;
}
我会帮助你。
答案 0 :(得分:0)
CreateJobObject(NULL, NULL)
方法需要save()
,因此您需要添加它,如下面的代码所示,带有注释:
transaction