Why use Facade pattern for EJB session bean

后端 未结 3 1625

I want to ask what is the reason to use Facade Pattern when access EJB Session Bean. In my Netbeans 6.9.1, if I do New > Sessions Bean for Entity Classes<

3条回答
  •  庸人自扰
    2020-12-15 23:44

    The point of using EJBs at all is that they provide features such as declarative transactions and security via annotations (or XML configurations). If you don't use those features, there is no point in having EJBs.

    Additionally, that autogenerated facade code is jsut a starting point (and a bad one). The EJBs should form a domain API, not a wrapper for all individual CRUD operations. They should only contain the operations you actually want to be performed on your domain model, and many of them should span several CRUD operations that need to be performend within a transaction. For example:

    @TransactionAttribute
    public void transferUser(User u, School from, School to){
        from.getUsers().remove(u);
        to.getUsers().add(u);
        u.setSchool(to);
        getEntityManager().merge(from);
        getEntityManager().merge(to);
        getEntityManager().merge(u);
    }
    

    The app server will execute all operations within a transaction, which ensures, for example, that you cannot have a situation where an exception is thrown for some reason and you end up with User that is removed from one Schoold but not added to the other one.

提交回复
热议问题