persistence@glassfish.java.net

Injection of EntityManager doesn't work with separated Entity and Session Beans

From: Thibault Billet <tbillet_at_smaeur.eu>
Date: Thu, 1 Jul 2010 18:18:13 +0200

Hi,

I'm fairly new to glassfish and i can't manage to deploy properly my
enterprise application on it.
I'm using Glassfish 3.0.1 and JPA 2 with Hibernate 3.5.0 as a persistence
provider. My Ear used to deploy and initialize itself just fine on Jboss 6
M2, but for some reason i wanted to migrate to Glassfish.

Problem is, the EntityManager injection doesn't seem to work in my Dao
session bean

Here is the structure of my Ear:

Ear
________________
- META-INF
--- application.xml

- my-domain.jar
--- META-INF
------ persistence.xml
--- POJOs

- my-ejb-daos.jar
--- MyDAO

- library.jar

- my-web.war

my-ejb-daos.jar contain a reference to my-domain.jar in its manifest's
class-path entry.

I created a Connection pool which i can ping successfully and a jdbc
resource with the exact same name as the jta-data-source element in my
persistence.xml

persistence.xml :
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="MY-UNIT-NAME" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/MY-DATASOURCE</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>


Deployment fails just after the servlet startup. the servlet tries to load
some data from the domain via my DAO and it fails and throw this exception:

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for
unitName MY-UNIT-NAME

In my DAO, I just try to inject an EntityManagerFactory like this
@PersistenceContext(unitName="MY-UNIT-NAME")
protected EntityManager entityManager;

and then use it like in this example:
return entityManager.find(entityClass, id);

After reading this thread : (
http://forums.java.net/jive/thread.jspa?messageID=396967 ), I tried putting
persistence.xml in the META-INF folder of my-ejb-daos.jar but that doesn't
help.

Any idea why the persistence unit isn't resolved ?