简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM