不会创建或检索JPA条目

时间:2013-03-17 18:11:13

标签: java database jpa eclipselink entitymanager

我正在使用实体管理器创建JPA条目的程序。 在此过程中我没有任何错误

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

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

...

entityManager.close();

我创建了一个类似如下的程序来读取数据,但它没有返回任何数据, 查询返回空。可能是什么原因?

这是读取数据的程序:

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());
            }
        }
    }

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="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>

1 个答案:

答案 0 :(得分:1)

您已将eclipselink配置为在每次启动应用时删除并重新创建架构:

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

很明显,如果你运行第一个持有某些条目的应用程序,然后是第二个尝试读取第一个应用程序的应用程序,你就不会再找到任何东西,因为eclipselink会删除所有内容并重新创建每个模式时间。