2011-04-06 80 views
0

我目前正在使用Hibernate + JPA的项目。 我不记得我在项目中改变了什么,但每次我尝试实例化一个新的EntityManagerFactory时,它都清除了数据库中的所有数据。Persistence.createEntityManagerFactory清除整个数据库

这里是代码片段:

public abstract class GenericDAO<T> { 

protected Class<T> t; 
protected EntityManagerFactory managerFactory; 
protected EntityManager manager; 
protected Session hibernateSession; 

public GenericDAO(Class<T> t) { 
    this.t = t; 
    this.managerFactory = Persistence.createEntityManagerFactory("hibernatePersistence"); 
    this.manager = this.managerFactory.createEntityManager(); 
    this.hibernateSession = HibernateUtil.getSessionFactory().openSession(); 
} 

在包含 “Persistence.createEntityManagerFactory(” hibernatePersistence “)” 行,整个数据库被清除出去。

我竭尽全力解决这个问题......我希望你们能帮忙。

在此先感谢!

回答

2

在您的项目中找到hibernate.hbm2ddl.auto属性(可能是persistence.xml文件)并将其删除或将其值更改为validate。另请参见:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional

+0

'更新',或'验证'是好的。只要避免'drop-create'。 (+1) – Bozho 2011-04-06 21:31:01

+0

不,我试过这两个建议,没有任何帮助,任何其他的想法? – 2011-04-07 01:25:18

+0

也许你正在使用内存数据库? H2/HSQLDB?什么是您的JDBC URL? – 2011-04-07 06:08:06

0

解决通过删除并创建一个全新的persistence.xml。不知道为什么这个问题发生,但从来不知道,它现在的作品...