Jean Tennie
Jean Tennie

Reputation: 253

JPA entries are not created or retrieved

I'm having program that creates JPA entries with the entity manager. during the process I don't get any errors.

...
factory = Persistence.createEntityManagerFactory("perst");
EntityManager entityManager = factory.createEntityManager();

entityManager.getTransaction().begin();
entityManager.persist(object);
entityManager.getTransaction().commit();

...

entityManager.close();

I have created a program like follows to read the data but it doesn't return any data, the query returns empty. What could be the reason ?

This is the program to read the data:

static List<String> classList = new ArrayList<String>();
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        factory = Persistence.createEntityManagerFactory("perst");
        EntityManager entityManager = factory.createEntityManager();

        classList.add("LeaveRequest");
        classList.add("person");

        for (Object classOjc : classList) {

            String className = classOjc.toString();

            Query query = entityManager.createQuery("SELECT p FROM " + className + " p");

            @SuppressWarnings("rawtypes")
            List resultList = query.getResultList();

            System.out.println(resultList.size());

            for (Object result : resultList) {

                System.out.println(result.toString());
            }
        }
    }

the xml persist is:

<?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="perst" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>LeaveRequest</class>
        <class>person</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" />
            <property name="javax.persistence.jdbc.user" value="Sales" />
            <property name="javax.persistence.jdbc.password" value="" />
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.logging.exceptions" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Upvotes: 1

Views: 173

Answers (1)

JB Nizet
JB Nizet

Reputation: 692151

You configured eclipselink to drop and recreate the schema each time the app is started:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

So obviously, if you run a first application that persists some entries, and then a second application which tries to read what the first one has written, you won't find anything anymore, since eclipselink drops everything and recreates the schema every time.

Upvotes: 1

Related Questions