2 Replies Latest reply on Jun 20, 2006 3:50 AM by hoagiex

    Is Generic Collection retrieval possible?

    hoagiex

      I'm trying to convert my old Hibernate GenericDAO to an EJB3.0 GenericDAO. My problem is that EJB3.0 requires a specific Query to retrieve all objects rather then a Criterion (like Hibernate) that is purely defined by a class type.

      Hibernate could do it like this:

       protected List<I>
       findAll()
       throws DAOException
       {
       List<I> myList;
      
       try
       {
       Criteria aCriteria =
       getSession().createCriteria(I);
       myList = aCriteria.list();
       }
       catch(HibernateException anException)
       {
       throw new DAOException(anException);
       }
       catch(Exception anException)
       {
       throw new DAOException(anException);
       }
      
       return myList;
       }
      


      My question is: Can the EntityManager (or some other service object) use something other then a query to obtain all objects from a table?



      public abstract class
      GenericEJBDAO<I, T, ID>
      implements GenericDAO<I, ID>
      {
       //--------------------------------------------------------------------
       // Constructors
       //--------------------------------------------------------------------
      
       protected GenericEJBDAO(Class<I> aPersistanceInterface,
       Class<T> aPersistanceClass)
       {
       thePersistanceInterface = aPersistanceInterface;
       thePersistanceClass = aPersistanceClass;
       }
      
       //--------------------------------------------------------------------
       // Public methods
       //--------------------------------------------------------------------
      
       public Collection<T>
       findAll()
       throws DAOException
       {
       try
       {
       ...?
       }
       catch(Exception anException)
       {
       throw new DAOException(anException);
       }
       }
      
       public I
       findByID (ID anID)
       throws DAOException
       {
       if(anID == null)
       throw new IllegalArgumentException("ID cannot be null");
      
       Object myPersistantObject = theManager.find(thePersistanceClass, anID);
       return thePersistanceInterface.cast(myPersistantObject);
       }
      
       public void
       delete(ID anID)
       throws DAOException
       {
       if(anID == null)
       throw new IllegalArgumentException("ID cannot be null");
      
       Object myPersistantObject = theManager.find(thePersistanceClass, anID);
       theManager.remove(myPersistantObject);
       }
      
       //--------------------------------------------------------------------
       // Members
       //--------------------------------------------------------------------
      
       @PersistenceContext
       protected EntityManager theManager;
      
       private Class<T> thePersistanceClass;
      
       private Class<I> thePersistanceInterface;
      


        • 1. Re: Is Generic Collection retrieval possible?
          echon

          Hi,

          because EJB is implemented via Hibernate in the Background you can try this:

          public abstract class GenericDAOImpl<T, ID extends Serializable> implements
           GenericDAO<T, ID> {
          
           private static final Log _log = LogFactory.getLog(GenericDAOImpl.class);
          
           @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "portalEM")
           protected EntityManager em;
          
           private Class persistentClass;
          
           public GenericDAOImpl(Class persistentClass) {
           this.persistentClass = persistentClass;
           }
          
           public Class getPersistentClass() {
           return persistentClass;
           }
          
           @SuppressWarnings("unchecked")
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           public T findByPrimaryKey(ID id) {
           try {
           return (T) em.find(getPersistentClass(), id);
           } catch (RuntimeException e) {
           _log.error("Exception while findByPrimaryKey", e);
           throw e;
           }
           }
          
           @SuppressWarnings("unchecked")
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           public List<T> findAll() {
           try {
           return em.createQuery("from " + getPersistentClass())
           .getResultList();
           } catch (RuntimeException e) {
           _log.error("Exception while findAll", e);
           throw e;
           }
           }
          
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           public T makePersistent(T entity) {
           try {
           return em.merge(entity);
           } catch (RuntimeException e) {
           _log.error("Exception while makeTransient", e);
           throw e;
           }
           }
          
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           public void makeTransient(T entity) {
           try {
           em.remove(entity);
           } catch (RuntimeException e) {
           _log.error("Exception while makeTransient", e);
           throw e;
           }
           }
          
           @SuppressWarnings("unchecked")
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           public List<T> findByCriteria(
           org.hibernate.criterion.Criterion... criterion) {
          
           try {
          
           org.hibernate.Session session = ((org.jboss.ejb3.entity.HibernateSession) em)
           .getHibernateSession();
           org.hibernate.Criteria crit = session
           .createCriteria(getPersistentClass());
          
           for (org.hibernate.criterion.Criterion c : criterion) {
           crit.add(c);
           }
          
           return crit.list();
          
           } catch (RuntimeException e) {
           _log.error("Exception while findByCriteria", e);
           throw e;
           }
           }
          
           @Remove
           public void remove() {
           }
          
          }
          


          Because this is not defined in EJB-Spec. don't try on other App-Server.
          EJB Spec. doesn't define something like a Criteria-API

          Regards

          Peter

          • 2. Re: Is Generic Collection retrieval possible?
            hoagiex

            Sweet thx. (backdoors are always nice to know)

            For now I'll stick with:

            em.createQuery("find "+getPersistentClass().getName()+" object").getResultList();
            


            ... to maintain a cross platform freedom. It's a lame solution, but probably the only one that's 'clean'.