简体   繁体   中英

JPA with Java SE: javax.persistence.PersistenceException: No Persistence provider for EntityManager

Executing a Java SE JPA standalone application throws the exception:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named test

I have found several similar posts but I think that none of them applies to my case with EclipseLink, Java SE and no packaging into deployable files and hence no WEB-INF etc.

The /src/META-INF/persistence.xml is:

<persistence>
    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>SomeEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/somedbname"/>
            <property name="javax.persistence.jdbc.user" value="foo"/>
            <property name="javax.persistence.jdbc.password" value="bar"/>
        </properties>
    </persistence-unit>
</persistence>

The following jars are on the build path:

  • org.eclipse.persistence.jpa-2.6.4.jar
  • eclipselink-2.6.4.jar
  • javax.persistence-2.1.1.jar
  • postgresql-9.4.1212.jar

您必须将eclipselink jar添加到您的ClassPath中,以检查它是否已经存在:

System.out.println(System.getProperty("java.class.path"));

我必须在xmlns属性的persistence.xml中提供持久性名称空间

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM