When I firstly tried EJB 3.0 in my applications I found that I need to write a log of EntityManager-like classes. They are very similar to DAO objects and each of they holds all possible operations for his entity. My app was full of similar lines of code. So I found some solutions on the Internet and combined them with my own thoughts. The following class is a result of several tries to implement the best solution:

package ccgproj.backend.rdbms.em;

import java.lang.reflect.ParameterizedType;

import java.util.ArrayList;

import java.util.List;

import javax.ejb.TransactionAttribute;

import javax.ejb.TransactionAttributeType;

import javax.ejb.TransactionManagement;

import javax.ejb.TransactionManagementType;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;

import ccgproj.backend.exceptions.CreateException;

import ccgproj.backend.exceptions.DataStoreException;

import ccgproj.backend.exceptions.FinderException;

import ccgproj.backend.exceptions.RemoveException;

import ccgproj.backend.exceptions.UpdateException;

import ccgproj.backend.rdbms.entity.IEntity;

/**

* Class type: base class for entityManager EJB's<br/>

* <br/>

* Orders:<br/>

* 1) provides base methods for managing entity <br/>

* 2) provides protected methods for executing queries <br/>

*

* @author sorokoumov

*/

@TransactionManagement(TransactionManagementType.CONTAINER)

@TransactionAttribute(TransactionAttributeType.REQUIRED)

public abstract class GenericManager<T extends IEntity> {

/**

* injected JPA entity manager

*/

@PersistenceContext(unitName = "CCGProjectPersistenceUnit")

private EntityManager em;

/**

* creates entity

*

* @param obj

* @return

* @throws CreateException

*/

public T create(T obj) throws CreateException {

try {

em.persist(obj);

return obj;

} catch (Exception e) {

throw new CreateException(e);

}

}

/**

* updates entity

*

* @param obj

* @return

* @throws UpdateException

*/

public T update(T obj) throws UpdateException {

try {

obj = em.merge(obj);

return obj;

} catch (Exception e) {

throw new UpdateException(e);

}

}

/**

* refreshes entity(this action completely resets a state of entity)

* @param obj

* @throws DataStoreException

*/

public void refresh(T obj) throws DataStoreException {

em.refresh(obj);

}

/**

* removes entity but it refreshes it before.

* Refreshing is required because entity could be detached/or composed out of transaction.

* @param obj

* @throws RemoveException

*/

public void remove(T obj) throws RemoveException {

try {

em.refresh(obj);

em.remove(obj);

} catch (Exception e) {

throw new RemoveException(e);

}

}

/**

* checks if entityManager contains this entity

* @param obj

* @return

* @throws DataStoreException

*/

public boolean contains(T obj) throws DataStoreException {

try {

return em.contains(obj);

} catch (Exception e) {

throw new DataStoreException(e);

}

}

/**

* detaches entity.

* It's very useful when you need to apply some changes to entity within a transaction

* but you don't want the container to call merge method because you want to perform a custom update to this entity.