0

We have an EAR application that contains a WAR, Utility project and a JPA project. The JPA project contains all our entity classes and in the META-INF directory is our persistence.xml which is defined as follows:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
    <persistence-unit name="TestJPA">
        <description>openjpa</description>
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>jdbc/TestSource</jta-data-source>
        <class>model.TestTable1</class>
        <class>model.TestTable2</class>
    </persistence-unit>
</persistence>

Our web.xml contains the following for the persistence unit.

  <persistence-unit-ref>
    <persistence-unit-ref-name>peristence/TestJPA</persistence-unit-ref-name>
    <persistence-unit-name>TestJPA</persistence-unit-name>
  </persistence-unit-ref>

In our webservice we have the following:

@PersistenceContext(name="peristence/TestJPA", unitName="TestJPA")
private EntityManagerFactory emf;

When the application is deployed emf never gets instantiated. Its always null. We've tried @PersistenceUnit as well, with and without arguments.

We've also tried a different approach by using InitialContext lookup to get an EntityManager. Using this approach an exception gets thrown that it can't find the resource name.

Our last approach we tried was the following:

em = (EntityManager) Persistence.createEntityManagerFactory("TestJPA");

Which produces the following exception

java.lang.ClassCastException: com.ibm.ws.persistence.EntityManagerFactoryImpl incompatible with javax.persistence.EntityManager

I've looked for all sorts of tutorials with regard to OpenJPA deployment with an EAR, but all of them are either for JPA 2 or for OpenJPA 1 with Spring.

Any help would be appreciated.

Burmudar
  • 263
  • 5
  • 14
  • Check the compatibilities. Are you importing from libraries that are compatible with each other. Furthermore, are you sure the EntityManagerFactoryImpl that you are using is compatible with your EntityManager?..... I have seen issues like this when using external libraries in WAS. – mhan Jul 11 '12 at 14:04
  • I am currently using the libraries which are added by the OpenJPA plugin for eclipse. – Burmudar Jul 11 '12 at 14:10
  • I would guess that your application contains `javax.persistence` classes and that you've set class loader delegation to "parent last". Is that right? – Brett Kail Jul 11 '12 at 16:18

2 Answers2

0

//try this

@PersistenceContext(name="TestJPA") private EntityManager em;

I'll suggest to dig into your WAS 7 and use those libraries. WAS is a nightmare if you use non-ibm libraries, even the JDK

mhan
  • 373
  • 3
  • 11
0

In web modules you should use PersistenceUnit instead of PersistneceContext. Try the following injection:

@PersistenceUnit(unitName="TestJPA")
EntityManagerFactory emf;

...
EntityManager em = emf.createEntityManager();
...
em.close();

Also what kind of web service are you using? If you are using third party WS engine, annotation integration might not work.

Gas
  • 17,601
  • 4
  • 46
  • 93