Is Generic Collection retrieval possible?
hoagiex Jun 19, 2006 9:38 AMI'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;