Search code examples
mavenjpaintellij-ideaglassfish-3ejb-3.1

EJB Maven module using JPA


I have simple maven EJB module using JPA. This is my persistance.xml file

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="Persistence">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>GroupTypes.xml</mapping-file> 
        <properties>
            <property name="hibernate.connection.url"  value="jdbc:oracle:thin:@127.0.0.1:1521:E"/>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="hibernate.connection.username" value="username"/>
            <property name="hibernate.connection.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

I am using EJB Stateless Bean and I am trying to get all attributes from GroupTypes table. This is my bean implementation:

public class TestBean
{

    private GroupTypes GroupTypes;
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

   @WebMethod (operationName = "justTesting")
   public boolean justTesting(@WebParam (name = "param") String value)
   {
       try
       {
            entityManagerFactory = Persistence.createEntityManagerFactory("Persistance");
            entityManager = entityManagerFactory.createEntityManager();
            Query query = entityManager.createQuery("Select name from GroupTypes");
            List<AmmEdGroupTypes> result = query.getResultList();
            return true;

        }
        catch(Exception e)
       {
            e.printStackTrace();
            return false;
       }
    }
}

When I try to invoke this method I get exeption: javax.persistence.PersistenceException: No Persistence provider for EntityManager named Persistance. My persistance.xml file is placed in folder resources/META-INF/persistance.xml, and if I am not using bean this solution works. Does anydbody have some idea why this is happening only in case when I am using bean?

I am using Intellij 12.1.1, Oracle 11g, Glassfish 3.1 server and JAVA 1.6.


Solution

  • I solved this problem in the following way:

    1. Installed hibernate add-on on glassfish (using glassfish update tool)
    2. On glassfish added jdb connection pool _AmmPool with properties: className, username,password,databaseName and url
    3. On glassfish added jdbc resource jdbc/__amm binding with connection pool _AmmPool
    4. Changed persistence.xml file
    5. Changed pom.xml file
    6. Copied the following libraries into glassfish domains/domain1/lib folder:

      • antlr-2.7.7
      • commons-collections-3.2.1,
      • dom4j-1.6.1,
      • ejb3-persistence-1.0.2.GA,
      • hibernate3-maven-plugin-3.0,
      • hibernate-commons-annotations-4.0.2.Final,
      • hibernate-core-4.2.2.Final,
      • hibernate-entitymanager-4.2.2.Final,
      • javassist-3.15.0-GA,
      • jta-1.1,
      • log4j-1.2.17,
      • oracle-jdbc-11.1.0.6.0-Production+,
      • slf4j-api-1.6.1
      • slf4j-log4j12-1.7.5

    A also copied these libraries into glassfish/lib floder.

    This is my new persistance.xml file: http://pastebin.com/xbEU9Tr0

    And this is my pom.xml file: http://pastebin.com/uEjhW36