使用hibernate在JPA中配置EntityManager

时间:2013-08-12 07:07:01

标签: java hibernate java-ee jpa

我的应用程序的META-INF文件夹中有以下persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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="Hello" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@my.db.com:1521:TEST"/>
            <property name="javax.persistence.jdbc.user" value="newuser" />
            <property name="javax.persistence.jdbc.password" value="password@123" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

web.xml的context-param是:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/rules-*.xml
    </param-value>
</context-param>

在代码中,我正在执行以下操作:

EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("Hello");
EntityManager em = emf.createEntityManager();
RulesMasterTable rule = em.find(RulesMasterTable.class,
        ruleName);

但例外是:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named Hello javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)

为什么我的持久单元没有注册?

1 个答案:

答案 0 :(得分:1)

我认为您不应该使用RESOURCE_LOCAL交易类型,请使用JTA。试着看这里:Difference between a "jta-datasource" and a " resource-local " datasource?