使用JPA持久化对象时获取javax.persistence.PersistenceException

时间:2014-07-23 12:11:51

标签: java jpa

我是Stackoverflow的新手。虽然使用JPA持久保存我的java对象但是会收到名为“javax.persistence.PersistenceException”的异常。 我的Java代码仍然存在:

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("camel");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        Book book = new Book();
        book.setAuthor("Raju");
        book.setTitle("Kiss Me");
        book.setGenre("Romance");
        em.persist(book);
        em.getTransaction().commit();

        em.close();

我的Persistence.xml是:

<persistence-unit name="camel">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>com.labs.Book</class>

    <properties>
        <property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/postgres" />
        <property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
        <property name="openjpa.ConnectionUserName" value="postgres" />
        <property name="openjpa.ConnectionPassword" value="raju" />
        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
        <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
    </properties>
</persistence-unit>

以下是异常的堆栈跟踪:

Exception in thread "main" javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "camel" after trying the following discovered implementations: org.apache.openjpa.persistence.PersistenceProviderImpl from provider: org.apache.openjpa.persistence.PersistenceProviderImpl
    at javax.persistence.Persistence.createPersistenceException(Persistence.java:244)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:186)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:72)
    at com.labs.App.main(App.java:48)
Caused by: <openjpa-2.3.0-r422266:1540826 nonfatal general error> org.apache.openjpa.util.GeneralException: org.xml.sax.SAXException: file:/C:/Users/omandala/EIP_Workspace/validation/target/classes/META-INF/persistence.xml [Location: Line: 1, C: 32]: org.xml.sax.SAXParseException; systemId: file:/C:/Users/omandala/EIP_Workspace/validation/target/classes/META-INF/persistence.xml; lineNumber: 1; columnNumber: 32; cvc-elt.1: Cannot find the declaration of element 'persistence-unit'.
    at org.apache.openjpa.lib.meta.XMLMetaDataParser.parseNewResource(XMLMetaDataParser.java:427)
    at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:347)
    at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:324)
    at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:297)
    at org.apache.openjpa.persistence.PersistenceProductDerivation$ConfigurationParser.parse(PersistenceProductDerivation.java:811)
    at org.apache.openjpa.persistence.PersistenceProductDerivation.parseResources(PersistenceProductDerivation.java:576)
    at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:528)
    at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:339)
    at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:86)
    at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:154)
    at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:62)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:152)
    ... 2 more

感谢您的时间和帮助。

0 个答案:

没有答案