JavaEE6 DAO: Should it be @Stateless or @ApplicationScoped?

前端 未结 3 1381
误落风尘
误落风尘 2020-12-08 03:21

I\'m currently creating an EJB3 Data Access Class to handle all database operations in my Java EE 6-application. Now, since Java EE 6 provides the new ApplicationScope

3条回答
  •  广开言路
    2020-12-08 03:31

    After some rethinking, it seems like DAO is actually not the right name for what i wanted to do. Maybe it really is a Facade, as Pascal said. I just found the Netbeans Petstore Example - a JavaEE6 sample application, see here - where they have an ItemFacade which is responsible for finding/createing/removing entities from the database. It's a Stateless Session Bean. Looks like this:

    @Stateless
    public class ItemFacade implements Serializable {
        @PersistenceContext(unitName = "catalogPU")
        private EntityManager em;
    
        public void create(Item item) { ... }
        public void edit(Item item) { ... }
        public void remove(Item item) { ... }
        public Item find(Object id) { ... }
        public List findAll() { ... }
        public List findRange(int maxResults, int firstResult) { ... }
        public int getItemCount() { ... }
    }
    

    So as a conclusion i don't call my DAO DAO anymore but instead just for example PersonEJB (i think "PersonFacade" could be misunderstood) and make it also @Stateless, as i think the Netbeans example can be considered as well-designed.

提交回复
热议问题