Skip to main content

Ear deployment and EntityManager injection

4 replies [Last post]
tibill
Offline
Joined: 2010-07-01

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 :

org.hibernate.ejb.HibernatePersistence
jdbc/MY-DATASOURCE

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 viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
batzee
Offline
Joined: 2006-11-28

I think that my-domain.jar and maybe also library.jar should be placed in the /lib subdirectory of your EAR. Don't forget to update the classpath entry in your DAO's manifest file.

persistence.xml definitely needs to be in domain.jar, not in DAO jar!

Message was edited by: batzee

tibill
Offline
Joined: 2010-07-01

Thanks batzee for your help, your suggestion didn't resolved directly my problem but i'm sure my project packaging is much more cleaner now.

For anyone in the same situation, the issue was that I was injecting both an EntityManager and an EntityManagerFactory in the same DAO.

It looks like Jboss and Glassfish have a different implementation for this particulary case.

Mitesh Meswani

>For anyone in the same situation, the issue was that I was injecting both an EntityManager and an EntityManagerFactory in the same DAO.

That should work. Can you please provide details on code you had to
change to make the problem go away.
On 7/2/2010 9:19 AM, glassfish@javadesktop.org wrote:
> Thanks batzee for your help, your suggestion didn't resolved directly my problem but i'm sure my project packaging is much more cleaner now.
>
> For anyone in the same situation, the issue was that I was injecting both an EntityManager and an EntityManagerFactory in the same DAO.
>
> It looks like Jboss and Glassfish have a different implementation for this particulary case.
> [Message sent by forum member 'tibill']
>
> http://forums.java.net/jive/thread.jspa?messageID=476667
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

tibill
Offline
Joined: 2010-07-01

Here is the original code:

[code]import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

import com.sma.bam.core.common.constants.CoreConstants;
import com.sma.bam.core.dal.local.IDao;

/**
* JPA implementation of DAO's
*
* @param
* The type to use as key
* @param
* The type of the entity
*/
public abstract class AbstractJpaDao implements
IDao {

/**
* The entity class
*/
protected Class entityClass;

/**
* The persistence entity manager
*/
@PersistenceContext(unitName = CoreConstants.PERSISTENCE_UNIT)
protected EntityManager entityManager;

@PersistenceUnit(unitName = CoreConstants.PERSISTENCE_UNIT)
protected EntityManagerFactory emf;

protected CustomPersistentBeanManager beanManager;

/**
* Class constructor
*/
public AbstractJpaDao() {
initializeEntityClass();
}

/**
* Class using Java's ParameterizedType class to obtain
* information about the declared generic type.
*/
@SuppressWarnings("unchecked")
public void initializeEntityClass() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
Type type = genericSuperclass.getActualTypeArguments()[1];
if (type instanceof ParameterizedType) {
this.entityClass = (Class) ((ParameterizedType) type).getRawType();
} else {
this.entityClass = (Class) type;
}
}

/* (non-Javadoc)
* @see com.sma.bam.core.dal.dao.Dao#persist(java.lang.Object)
*/
@Override
public void persist(E entity) {
entityManager.persist(entity);
}

...
}[/code]

And here is the workaround code:

[code]import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

import com.sma.bam.core.common.constants.CoreConstants;
import com.sma.bam.core.dal.local.IDao;

/**
* JPA implementation of DAO's
*
* @param
* The type to use as key
* @param
* The type of the entity
*/
public abstract class AbstractJpaDao implements
IDao {

/**
* The entity class
*/
protected Class entityClass;

/**
* The persistence entity manager
*/
protected EntityManager entityManager;

@PersistenceUnit(unitName = CoreConstants.PERSISTENCE_UNIT)
protected EntityManagerFactory emf;

/**
* Class constructor
*/
public AbstractJpaDao() {
initializeEntityClass();
}

/**
* Class using Java's ParameterizedType class to obtain
* information about the declared generic type.
*/
@SuppressWarnings("unchecked")
public void initializeEntityClass() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
Type type = genericSuperclass.getActualTypeArguments()[1];
if (type instanceof ParameterizedType) {
this.entityClass = (Class) ((ParameterizedType) type).getRawType();
} else {
this.entityClass = (Class) type;
}
}

@PostConstruct
public void initialize {
entityManager = emf.createEntityManager();
}

/* (non-Javadoc)
* @see com.sma.bam.core.dal.dao.Dao#persist(java.lang.Object)
*/
@Override
public void persist(E entity) {
entityManager.persist(entity);
}

...
}[/code]