Persistence.createEntityManagerFactory 清除整个数据库
我目前正在处理一个使用 Hibernate + JPA 的项目。 我不记得我在项目中更改了什么,但每次我尝试实例化一个新的 EntityManagerFactory 时,它都会清除数据库中的所有数据。
下面是代码片段:
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")”的行中,整个数据库被清除。
我用尽了解决这个问题的所有想法......我希望你们能提供帮助。
提前致谢!
I'm currently working with a project using Hibernate + JPA.
I don't recall exactly what I changed in the project, but everytime I try to instantiate a new EntityManagerFactory, it clears out all of the data from the database.
Here is the code snippet:
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();
}
In the line that contains "Persistence.createEntityManagerFactory("hibernatePersistence")", the whole database is cleared out.
I exhausted every idea for solving this issue... I hope you guys can help.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在项目中的某个位置(可能是 persistence.xml 文件)查找 hibernate.hbm2ddl.auto 属性并将其删除或将其值更改为 validate 。另请参阅:
http:// docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-可选
Look for
hibernate.hbm2ddl.auto
property somewhere in your project (probablypersistence.xml
file) and remove it or change its value tovalidate
. See also:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional
通过删除并创建全新的 persistence.xml 来解决。不知道为什么会出现这个问题,但没关系,现在它可以工作了......
Resolved by deleting and creating a fresh new persistence.xml. Don't know why this problme occured, but nevermind, it works now...