没有EntityManager,JPA配置的持久性提供程序

时间:2014-01-30 10:08:14

标签: java hibernate java-ee jpa

我尝试使用以下persistence.xml配置JPA Hibernate:

<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <mapping-file>orm.xml</mapping-file>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="x" />
        <property name="javax.persistence.jdbc.url"
            value="x;create=true" />
        <property name="javax.persistence.jdbc.user" value="x" />
        <property name="javax.persistence.jdbc.password" value="x" />
        <property name="dialect" value="org.hibernate.dialect.DB2Dialect" />
        <!-- EclipseLink should create the database schema automatically -->
        <!-- <property name="eclipselink.ddl-generation" value="create-tables" 
            /> <property name="eclipselink.ddl-generation.output-mode" value="database" 
            /> -->
    </properties>

</persistence-unit>

在“x”的地方有适当的值,但我得到了:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at de.volkswagen.webpromet.Main.main(Main.java:10)

感谢您的帮助! ! !

4 个答案:

答案 0 :(得分:2)

在MEAT_INF文件夹中添加persistence.xml 并以这种方式创建持久性

    <persistence version="2.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_2_0.xsd">
        <persistence-unit name="abc">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- map your classes. -->
    <properties>
    <property name="javax.persistence.jdbc.driver" value="x" />
    <property name="javax.persistence.jdbc.url"value="x/>
    <property name="javax.persistence.jdbc.user" value="x" />
    <property name="javax.persistence.jdbc.password" value="x" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    <property name="hibernate.show_sql" value="true" />
    </properties>
        </persistence-unit>
    </persistence>

创建一个java类HibernateUtil.java并将持久性单元名称提供给entityManagerFactory = Persistence.createEntityManagerFactory(“abc”);

public class HibernateUtil {

    private static final EntityManagerFactory entityManagerFactory;
    static {
                try {
                     entityManagerFactory = Persistence.createEntityManagerFactory("abc");
                     System.out.println("Entity Menager Test.............."+ entityManagerFactory);
                } catch (Throwable ex) {

                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);

                  }
    }

public static EntityManagerFactory getEntityManagerFactory() {
         return entityManagerFactory;
    }

}

尝试它会起作用。

答案 1 :(得分:1)

如果要将persistence.xml文件放在WEB-INF目录中,它永远不会进入类路径。将META-INF/persistence.xml文件添加到源文件夹的根目录。

答案 2 :(得分:1)

这个地方清楚地显示了应该放在哪里persistence.xml

提示:文档是最好的教程!

src
|-- main
|   |-- java
|   `-- resources
|       |-- jpa
|       |   |-- Clerk-orm.xml
|       |   |-- Customer-orm.xml
|       |   |-- Person-orm.xml
|       |   `-- Sale-orm.xml
|       `-- META-INF
|           `-- persistence.xml
`-- test
    `-- resources
        `-- hibernate.properties

更多信息:http://webdev.apl.jhu.edu/~jcs/ejava-javaee/coursedocs/605-784-site/docs/content/html/hibernate-migration-orm.html#hibernate-migration-orm-mapping

答案 3 :(得分:0)

您需要将persistence.xml文件放在适当的位置。 在您的情况下,将META-INF/persistence.xml文件添加到源文件夹的根目录。

以下内容来自 JPA规范

A persistence.xml file defines a persistence unit. The persistence.xml file is 
located in the META-INF directory of the root of the persistence unit. 

持久性单元的根是这里的关键。

由于您使用的是 Java EE应用,因此以下内容有效

In Java EE environments, the root of a persistence unit must be one of the following:
• an EJB-JAR file
• the WEB-INF/classes directory of a WAR file[80]
• a jar file in the WEB-INF/lib directory of a WAR file
• a jar file in the EAR library directory
• an application client jar file

但是,如果您要创建非Java EE应用,则以下内容适用:

The jar file or directory whose META-INF directory contains the persistence.xml 
file is termed the root of the persistence unit.