users@glassfish.java.net

Re: Ear deployment and EntityManager injection

From: <glassfish_at_javadesktop.org>
Date: Mon, 05 Jul 2010 01:21:33 PDT

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 <K>
 * The type to use as key
 * @param <E>
 * The type of the entity
 */
public abstract class AbstractJpaDao<K, E> implements
                IDao<K, E> {

        /**
         * The entity class
         */
        protected Class<E> 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<E>) ((ParameterizedType) type).getRawType();
                } else {
                        this.entityClass = (Class<E>) 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 <K>
 * The type to use as key
 * @param <E>
 * The type of the entity
 */
public abstract class AbstractJpaDao<K, E> implements
                IDao<K, E> {

        /**
         * The entity class
         */
        protected Class<E> 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<E>) ((ParameterizedType) type).getRawType();
                } else {
                        this.entityClass = (Class<E>) 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]
[Message sent by forum member 'tibill']

http://forums.java.net/jive/thread.jspa?messageID=476769