users@glassfish.java.net

Re: Having trouble getting JPA to work when migrating from Jboss to glassfish

From: Sahoo <Sahoo_at_Sun.COM>
Date: Wed, 28 Jan 2009 22:14:21 +0530

See reply inline...

glassfish_at_javadesktop.org wrote:
> I have multi component ejb project where each component is packaged as jar and the project is an EAR.I have my persitence.xml something like this:
>
> <?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="PU1" transaction-type="JTA">
> <provider>org.hibernate.ejb.HibernatePersistence</provider>
> <jta-data-source>dataSource1</jta-data-source>
> <properties>
> <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
> </properties>
> </persistence-unit>
>
>
> <persistence-unit name="PU2" transaction-type="JTA">
> <provider>org.hibernate.ejb.HibernatePersistence</provider>
> <jta-data-source>dataSource2</jta-data-source>
> <properties>
> <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.routing.xml"/>
> </properties>
> </persistence-unit>
> </persistence>
>
>
> i am using @PersistenceContext(name = "persistence/PU1", unitName = "PU1")
> javax.persistence.EntityManagerFactory eM
>
I am assuming this to be a typo. You probably wanted to write
EntityManager instead of EntityManagerFactory. If not, then you must fix
your code.
> in my staless bean.
>
> In my ejb-jar.xml i have mentioned
> <session>
> <ejb-name>SAMPLE</ejb-name>
> <local>com.package.SAMPLE</local>
> <remote>com.package.SAMPLERemote</remote>
> <ejb-class>com.package.SAMPLEBean</ejb-class>
> <session-type>Stateless</session-type>
> <transaction-type>Bean</transaction-type>
> <persistence-context-ref>
> <persistence-context-ref-name>persistence/PU1</persistence-context-ref-name>
> <persistence-unit-name>PU1</persistence-unit-name>
> </persistence-context-ref>
> </session>
>
>
I don't understand why you need al the above entries in ejb-jar.xml when
you are already using annotations. You should reconsider use of ejb-jar.xml.
> In a similar way i am using the other persistence unit and also i have configured the JDBC datasources in glassfish namely dataSource1 and dataSource2.
>
> But when i am trying to deploy the application i am getting following error:
>
> SEVERE: Exception occured in J2EEC Phase
> com.sun.enterprise.deployment.backend.IASDeploymentException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [persistence/PU1] in the scope of the module called []. Please verify your application.
>
The error indicates that the ejb module is not able to see the
persistence units. If you run "glassfish_home/bin/verifier <your ear
file>," you will get a better diagnostic message. The problem seems to
be that you have packaged your persistence units in some other ejb-jar
and you are trying to use them from another ejb-jar. As per the
visibility rules of persistence units, when a persistence unit is
packaged in a component jar like ejb-jar, it is considered private to
that jar. To fix your issue, you need to package your persistence units
in a library jar. Check out [1].

Sahoo

[1]
http://weblogs.java.net/blog/ss141213/archive/2005/12/using_java_pers.html