奇怪的 JPA 行为:应用程序启动时清理表
几乎所有内容都在标题中...当应用程序启动时,持久性工作正常,当应用程序关闭时,行仍在数据库中,但是一旦加载应用程序,行就会被删除...
我正在使用已经存在的数据库结构,通过mysql5 SGBD。
似乎来自我的实体经理声明。以下是我的代码的几行(实体管理器语句和 persistence.xml)
entityManager 语句:
entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("entityManager.persistenceUnit")).createEntityManager();
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("query.query"));
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="AnalysesPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider
<class>solianceanalysesmanager.Produits
<properties>
<property name="toplink.jdbc.user" value="XXX"/>
<property name="toplink.jdbc.password" value="X"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/Analyses"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
有人知道这个奇怪问题的原因吗? 我已经收到建议,我的实体管理器正在其声明中重新初始化持久性上下文...
PS:因为我是法国人,所以我可能错误使用或拼写错误了一些单词。请随时要求我重新表述。
Almost all is in the title... Persistence is working fine when app launched, and row are still in DB when the app is closed, but once app is loaded, rows got deleted...
I'm using an already existing database structure, through the mysql5 SGBD.
Seems to be coming from my entity manager declaration. Here are a few lines of my code, (entity manager statement, and persistence.xml)
entityManager statement:
entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("entityManager.persistenceUnit")).createEntityManager();
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("query.query"));
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="AnalysesPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider
<class>solianceanalysesmanager.Produits
<properties>
<property name="toplink.jdbc.user" value="XXX"/>
<property name="toplink.jdbc.password" value="X"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/Analyses"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
Does someone have an idea of this strange problem cause?
I already got suggested that my entityManager was reinitialising persistence context on its statement...
PS: Since i'm french, it's possible i mis-used or mis-spelled some words. Feel free to ask me to reformulate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,TopLink Essentials 不会重新创建表,但这可以在 persistence.xml 中进行配置。
看,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL
我猜你正在使用 Glassfish?我相信 Glassfish 在开发模式下默认会执行此操作,并且您需要配置一些 Glassfish 设置以避免这种情况。我不确定 Glassfish 中是如何配置的,也许其他人是这么做的。
TopLink Essentials does not recreate your tables by default, but this can be configured in your persistence.xml.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL
My guess is you are using Glassfish? I believe Glassfish does this by default in development mode, and there is some Glassfish setting you need to configure to avoid this. I'm not sure how this is configured in Glassfish though, perhaps someone else does.
因此,问题是:尽管有 persistence.xml 设置,Netbeans 的默认行为是自动“删除并创建”策略。
构建项目后,使用 .jar 将不会重置数据库表。
So, the problem was: Netbeans default behavior is an automatic "drop and create" strategy, despite the persistence.xml settings.
Once you build the project, using the .jar won't reset the databases table.
您的 persistence.xml 缺少一些属性定义...您必须向实体管理器指定如何行为,它将放置并创建参数作为默认设置。这就是为什么加载应用程序时所有行都会被删除。尝试添加此行来更改 jpa 行为:
your persistence.xml miss some properties definition...you have to specify to the entity manager how to behave, it's taking the drop and create parameters as default setting. that's why all rows are deleted when app is loaded.. try to add this line to change jpa behaviour :