JPA: configure persistence provider

2019-08-25 10:02发布

问题:

  • I got a simple java project created with maven (quickstart archetype)

  • I am trying to configure JPA persistence for drools sessions (the code comes from drools documentation)

    1. I added drools-persistence-jpa, Bitronix Transaction Manager and com.h2database dependencies to my pom.xml

    2. I created a META-INF folder as Source-Folder in my Eclipse Project in "src/META-INF"

    3. I added the persistence.xml and jndi.properties file there.

    4. In my TestCase I have following code:

    [...] EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa"); [...]

When running the test, I get the following Exception:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.drools.persistence.jpa at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at com.sample.MyTest.testJPA(MyTest.java:112)

I am relatively sure, that there's just something wrong with the way I created the META-INF or persistence.xml (see below). Any suggestions?

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
    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 
    http://java.sun.com/xml/ns/persistence/orm 
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
    <persistence-unit name="org.drools.persistence.jpa" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/testDatasource</jta-data-source>
        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.drools.persistence.processinstance.ProcessInstanceInfo</class>
        <class>org.drools.persistence.processinstance.ProcessInstanceEventInfo</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.autocommit" value="true" />
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
        </properties>
    </persistence-unit>
</persistence>

回答1:

I think the problem is related to the place where you put your persistence.xml file. Instead of src/META-INF you must place is either in src/main/resources/META-INF or src/test/resources/META-INF

Edited: In your persistence.xml file you are stating that you want to use org.hibernate.ejb.HibernatePersistence as a provider. According to your comments, you are not including hibernate-entitymanager as a dependency [source]. Try to add that dependency.

Hope it helps,