Search code examples
javapersistenceeclipselinkvaadinjpacontainer

Lack of persistence on server reset while using JPAContainer with Vaadin Framework


I'm using the JPAContainer implementation in my Vaadin (GWT based Java web application framework) application. I've been running into slight problems as the development is nearing to an end, all of which are to a degree related to the persistence (as in no data is actually put in the database and is all lost whenever I restart the server).

Here is where I define the entity manager from the PERSISTENCE_UNIT

private static JPAContainer<QueryableDicom> dicomDataContainer;    @PersistenceContext(unitName = "companynamedb")
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
transient EntityManager entityManager = entityManagerFactory.createEntityManager();

And this is how I instantiate my JPAContainer

dicomDataContainer = JPAContainerFactory.make(QueryableDicom.class, entityManager);

Here is my relevent part of my persistence.xml:

<persistence-unit name="companynamedb">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.jdbc.platform"
            value="org.eclipse.persistence.platform.database.H2Platform" />
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/./dcmCache" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="eclipselink.logging.level" value="FINE" />
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
        <property name="eclipselink.ddl-generation.output-mode"
            value="database" />
    </properties>
</persistence-unit>

I'm not sure if that'll help but here is how I add entities to the container:

entityManager.getTransaction().begin();
                    for (int i = 0; uploads[i] != null; i++) {
                        dicomDataContainer.addEntity(uploads[i]);
                    }
                    entityManager.getTransaction().commit();

I'm mostly looking for hints and tips on where to look, I've been googling around for two weeks now


Solution

  • You have your persistence unit set to drop and create all tables each and every time the persistence unit is deployed through the "eclipselink.ddl-generation" property value "drop-and-create-tables". Deployment occurs each time the server restarts, which explains why you lose data.

    This setting is only intended for development when there might be drastic entity changes that are not compatible with the previous schema. You might instead want to be using "create" or "create-or-extend-tables" as described here