Strange JPA behavior: tables cleared upon application loading

Almost everything is in the header ... Persistence works great when the application starts, and the row is still in the database when the application is closed, but as soon as the application loads, the rows are deleted ...

I am using an existing database structure through mysql5 SGBD.

It seems that I came from an entity manager declaration. Here are a few lines of my code, (entity manager instruction 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 anyone have an idea for this strange problem? I have already suggested that my entityManager reinitialize the persistence context on its approval ...

PS: Since I am French, I may have used or spoken some words incorrectly. Feel free to ask me to reformulate.

+4
source share
3 answers

TopLink Essentials does not recreate your tables by default, but you can configure this in the persistence.xml file.

See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL

I assume you are using Glassfish? I believe that Glassfish does this by default in design mode, and there are some Glassfish settings that you need to configure to avoid this. I'm not sure how this is set up in Glassfish, although maybe someone else does.

+2
source

So the problem was that the default behavior of Netbeans is an automatic drop and create strategy, despite the persistence.xml settings.

After creating the project, using .jar will not reset the database table.

+2
source

your persistence.xml will miss some property definition ... you need to tell the entity manager how to behave, it takes a drop value and creates the parameters as default settings. why all the lines are deleted when the application loads. Try adding this line to change the jpa behavior:

 <property name="toplink.ddl-generation" value="create-tables"/> 
+1
source

Source: https://habr.com/ru/post/1339797/


All Articles