I'm developing an EJB based application, in which I have the JpaUtility utility class. To implement it, I have two choices:
/* Simple Object utility */
public class JpaUtility
{
public static List<T> getAll(EntityManager manager, Class<T> clazz) {
return manager.createQuery("...."); }
}
/* EJB utility */
@Stateless
public class JpaUtility
{
@PersistentContext
private EntityManager manager;
public static List<T> findAll(Class<T> clazz) {
return manager.createQuery("...."); }
}
The question is: which one offers the best performance? Simple Object or EJB-Stateless? In other way, what does EJB offer compared to a simple object ?
What you're trying to accomplish looks like 'Generic Pattern DAO', there're many "ready" implementations out there.One from AdamBien: "Generic CRUD Service" More elaborate, eliminating the need for custom solution and approaching the simplicity of Spring templates from Apache DeltaSpike: DeltaSpike Data module
Now back to you original question, EJB or POJO, in your case, when working in an EE container the use of Entity manager, must be "container managed", so your "correct" options are EJB or CDI but not plain unmanaged POJO.