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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Thibault Billet
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 ? 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mitesh Meswani-2
Please shift my-domain.jar to lib/ folder of you .ear from root (See section 8.2 of JPA 2.0 spec for details). That should make it work.
On 7/1/2010 9:18 AM, Thibault Billet wrote:
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 ? 

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Thibault Billet
Thanks for the suggestion but that doesn't seem to be the problem :(
I did that and also updated the manifests classpath entries and also added <library-directory>lib</library-directory> to application.xml. Same Exception as before.


My Ear looks now like this:
Ear
________________
- META-INF/
--- application.xml

- lib/
--- my-domain.jar
------ META-INF/
--------- persistence.xml
------ POJOs
--- library.jar

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

- my-web.war

I'm running out of ideas

2010/7/2 Mitesh Meswani <[hidden email]>
Please shift my-domain.jar to lib/ folder of you .ear from root (See section 8.2 of JPA 2.0 spec for details). That should make it work.
On 7/1/2010 9:18 AM, Thibault Billet wrote:
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 ? 


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mitesh Meswani-2
You do not need any manifest entries with the structure below. Please run your application through verifier to see if it can find any obvious issues. If that does not work, please post a test ear that replicates the issue. I can take a look at what is going on.
On 7/2/2010 5:29 AM, Thibault Billet wrote:
Thanks for the suggestion but that doesn't seem to be the problem :(
I did that and also updated the manifests classpath entries and also added <library-directory>lib</library-directory> to application.xml. Same Exception as before.


My Ear looks now like this:
Ear
________________
- META-INF/
--- application.xml

- lib/
--- my-domain.jar
------ META-INF/
--------- persistence.xml
------ POJOs
--- library.jar

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

- my-web.war

I'm running out of ideas

2010/7/2 Mitesh Meswani <[hidden email]>
Please shift my-domain.jar to lib/ folder of you .ear from root (See section 8.2 of JPA 2.0 spec for details). That should make it work.
On 7/1/2010 9:18 AM, Thibault Billet wrote:
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 ? 



Loading...