用于简单CRUD的EJB 3会话Bean设计 [英] EJB 3 Session Bean Design for Simple CRUD

查看:199
本文介绍了用于简单CRUD的EJB 3会话Bean设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个唯一目的的应用程序是为了在数据库中维护记录进行CRUD操作。一些表/实体之间有关系。我见过的创建会话bean的大多数例子都涉及与我没有的许多实体进行交互的复杂业务逻辑/操作。

I am writing an application that's sole purpose in life is to do CRUD operations for maintaining records in database. There are relationships between some of the tables/entities. Most examples I've seen for creating session beans deals with complex business logic/operations that interact with many entities which I don't have.

由于我的应用程序非常基本的,会话bean的最佳设计是什么?

Since my application is so very basic, what would be the best design for the session bean(s)?

我在想,每个具有CRUD的实体有一个会话bean,定义了这个方法。然后我想到将所有这些会话bean组合成一个会话bean。然后我发现这个博客条目很有趣,但我必须承认我不明白(什么是ServiceFacade?)。

I was thinking of having one session bean per entity which had CRUD the methods defined. Then I thought of combining all of those session beans into a single session bean. And then I found this blog entry which is intriguing, but I must admit I don't understand all of it (what is a ServiceFacade?).

我倾斜对于会话bean /实体类,但是想听到更多有经验的意见。

I'm leaning towards session bean/entity class, but would like to hear more experienced opinions.

谢谢。

糟糕,这里是博客链接: http: //www.adam-bien.com/roller/abien/entry/generic_crud_service_aka_dao

Oops, here's the blog link: http://www.adam-bien.com/roller/abien/entry/generic_crud_service_aka_dao

推荐答案

不知道什么博客您在谈论的条目:)但是在您的特定情况 1 ,我可能会使用一个单一的会话bean实现类似于以下接口:

Not sure what blog entry you're talking about :) But in your particular situation1, I'd probably use a single session bean implementing an interface similar to:

public interface GenericCrudService {
    public <T> T create(T t);
    public <T> T find(Class<T> type, Object id);
    public <T> void delete(T t);
    public <T> T update(T t);
    public List findWithNamedQuery(String queryName);
    public List findWithNamedQuery(String queryName, int resultLimit);
    public List findWithNamedQuery(String namedQueryName, 
                                   Map<String, Object> parameters);
    public List findWithNamedQuery(String namedQueryName, 
                                   Map<String, Object> parameters,
                                   int resultLimit);
    public <T> List<T> findWithNativeQuery(String sql, Class<T> type);
}

该bean将如下所示:

And the bean would be as follow:

@Stateless
@Remote(GenericCrudService.class)
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class GenericCrudServiceBean implements GenericCrudService {
    @PersistenceContext
    private EntityManager em;

    @Override
    public <T> T create(T t) {
        em.persist(t);
        return t;
    }

    @Override
    public <T> T find(Class<T> type, Object id) {
        return em.find(type, id);
    }

    @Override
    public <T> void delete(T t) {
        t = em.merge(t);
        em.remove(t);
    }

    @Override
    public <T> T update(T t) {
        return em.merge(t);
    }

    @Override
    public List findWithNamedQuery(String queryName) {
        return em.createNamedQuery(queryName).getResultList();
    }

    @Override
    public List findWithNamedQuery(String queryName, int resultLimit) {
        return em.createNamedQuery(queryName).setMaxResults(resultLimit)
                .getResultList();
    }

    @Override
    public List findWithNamedQuery(String namedQueryName,
                                   Map<String, Object> parameters) {
        return findWithNamedQuery(namedQueryName, parameters, 0);          
    }

    @Override
    public List findWithNamedQuery(String namedQueryName,
                                   Map<String, Object> parameters,
                                   int resultLimit) {
        Query query = this.em.createNamedQuery(namedQueryName);
        if(resultLimit > 0) {
            query.setMaxResults(resultLimit);            
        }
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
        return query.getResultList();
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T>  List<T> findWithNativeQuery(String sql, Class<T> type) {
        return em.createNativeQuery(sql, type).getResultList();
    }
}



另见




  • 使用Java EE的通用CRUD组件5

  • 不要重复DAO! - 使用Hibernate和Spring DAO构建一个通用的类型安全DAO

  • See also

    • Generic CRUD Components with Java EE 5
    • Don't repeat the DAO! - Build a generic typesafe DAO with Hibernate and Spring DAO
    • 1 应用程序不应该将原始CRUD直接暴露给客户端,而是在执行业务规则并封装对域存储( EntityManager )的服务后面屏蔽CRUD。

      1 Most application shouldn't expose raw CRUD directly to clients but shield CRUD behind services implementing business rules and encapsulating access to Domain Stores (the EntityManager).

      这篇关于用于简单CRUD的EJB 3会话Bean设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆