开发者

Strange JPA behaviour: tables cleaned at app boot

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 开发者_Go百科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.


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.


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.


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 :

<property name="toplink.ddl-generation" value="create-tables"/>  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜