Search code examples
javajakarta-eeejbpojostateless

Utility class : EJB Session or Simple Object?


I'm developing an EJB based application, in which I have the JpaUtility utility class. To implement it, I have two choices:

  • Create a simple object JpaUtility, in which I put the getAll method static, to enable access from the class name (JpaUtility.getAll)

/* Simple Object utility */

public class JpaUtility
{
public static List<T> getAll(EntityManager manager, Class<T> clazz) {
        return manager.createQuery("...."); }
}
  • Create an EJB-Session that contains the getAll method, then inject and use when needed.

/* 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 ?


Solution

  • 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.