【问题标题】:Persistence.createEntityManagerFactory clearing out the whole databasePersistence.createEntityManagerFactory 清除整个数据库
【发布时间】:2011-04-06 19:37:55
【问题描述】:

我目前正在处理一个使用 Hibernate + JPA 的项目。 我不记得我在项目中具体做了什么更改,但是每次我尝试实例化一个新的 EntityManagerFactory 时,它都会清除数据库中的所有数据。

这里是sn-p的代码:

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")"的行中,整个数据库被清空。

我已经用尽了解决这个问题的所有想法......希望你们能提供帮助。

提前致谢!

【问题讨论】:

    标签: hibernate jpa hibernate-entitymanager


    【解决方案1】:

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

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

    【讨论】:

    • updatevalidate 都可以。避免drop-create。 (+1)
    • 不,我尝试了这两个建议,但没有任何帮助,还有其他想法吗??
    • 也许您正在使用内存数据库? H2/HSQLDB?你的 JDBC URL 是什么?
    • jdbc:mysql://127.0.0.1/cafeina 顺便说一下jdbc URL
    • 通过删除并创建一个全新的 persistence.xml 解决了这个问题。我不知道它为什么会发生以及它是如何发生的,但现在它起作用了。谢谢你的光!
    【解决方案2】:

    通过删除并创建新的 persistence.xml 解决。不知道为什么会出现这个问题,但没关系,它现在可以工作了......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多