shivraj
shivraj

Reputation: 1

No Persistence provider for EntityManager named xxx

Inside drools workbench i created java class to get entitymanager and i used inside drl files ,But my problem is that

  1. when i create new project in workbench i create project structure like maven
  2. the persistence.xml is inside src/main.resources/METE-INf/persistence.xml

when i call class to get entitymanager like bellow

package demo.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.hibernate.Session;

/**
 * This class was automatically generated by the data modeler tool.
 */

public class Factory implements java.io.Serializable {

    static final long serialVersionUID = 1L;

    public Factory() {
    }


public Session getSessionHibernate(){

  HibernateEntityManagerFactory ha = (HibernateEntityManagerFactory) Persistence
                .createEntityManagerFactory("flightdataunit");
        EntityManager emf = ha.createEntityManager();

        Session session = emf.unwrap(Session.class);

        session.beginTransaction().begin();
        return session;
}
}

And persistence.xml like bellow

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence 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" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
    <persistence-unit name="flightdataunit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/sqlserver/flightdata</jta-data-source>
        <class>demo.test.Aircraftfleetdetail</class>
        <class>demo.test.Flightdatacrew</class>
        <class>demo.test.Airfield</class>
        <class>demo.test.FlightdatacrewPK</class>
        <class>demo.test.Flighttype</class>
        <class>demo.test.Repository</class>
        <class>demo.test.Seatconfig</class>
        <class>demo.test.Aircraftidentity</class>
        <class>demo.test.Place</class>
        <class>demo.test.Flightdata</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.id.new_generator_mappings" value="false"/>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

and pom.xml like

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>TEST</artifactId>
  <version>1.0</version>
  <name>TEST</name>
  <dependencies>
    <dependency>
      <groupId>sqlserver</groupId>
      <artifactId>jarfile</artifactId>
      <version>4.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.1.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0.2</version>
    </dependency>
    <dependency>
      <groupId>antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>2.7.6</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.common</groupId>
      <artifactId>hibernate-commons-annotations</artifactId>
      <version>5.0.1.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.1.0.Final</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.kie</groupId>
        <artifactId>kie-maven-plugin</artifactId>
        <version>6.3.0.Final</version>
        <extensions>true</extensions>
      </plugin>
    </plugins>
  </build>
</project>

when i tried to call Factory class i got no persistence provider for named XXX

new factory class

package demo.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.hibernate.Session;
import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;
/**
 * This class was automatically generated by the data modeler tool.
 */

@Stateless                                                                            
public class Factory implements java.io.Serializable {

    static final long serialVersionUID = 1L;

    @PersistenceContext(unitName ="flightdataunit")
    EntityManager entityManager;

    public Factory() {
    }


public Session getSessionHibernate(){

        Session session = entityManager.unwrap(Session.class);


        return session;
}
}

Upvotes: 0

Views: 765

Answers (1)

rapasoft
rapasoft

Reputation: 961

You are mixing two different things. If you are using Java EE compliant application server you can actually inject the EntityManager using @PeristenceContext annoation. If you want to use JPA, don't try to create hibernate session (e.g. vendor specific implementation), and definitely do not start transactions on your own - use container managed transactions.

So you will need to

  • make Factory a @Stateless session bean (or stateful, depends on usage)
  • Inject @PersistenceContext EntityManager em in Factory
  • In your code call methods of em, e.g. the EntityManager

This is a nice resource to put you right on track: http://www.tutorialspoint.com/ejb/ejb_persistence.htm

Upvotes: 1

Related Questions