7 Replies Latest reply on Apr 10, 2006 5:23 AM by kh2ouija

    ejb3 best practice question

    kh2ouija

      Hi,

      I have a set of entitites deployed on the server, with stateless session facade beans for communications with remote clients (using value objects). My entity-specific DAOs were injected in the session facades using @EJB annotation.
      I'm now in position of using this model in a web-app that will be deployed on the same server. Now, I was hoping that if the client app is now residing on the same server (same VM and everything) I would now have direct access to the model entities graph and that I wouldn't have to use value objects anymore. But I was unable to inject the DAOs in my (JSF) view beans; if I obtain them by JNDI, I still can't use lazy collections transparently so that means that I haven't made my life any easier and I could just as well use my old value-objects based interfaces.

      Sorry for the long post, hope you're still with me. My questions are:

      0) am I doing anything wrong?
      1) can I inject my DAOs in my web-app's view beans? how?
      2) can I use the model graph in my web-app directly, without having to do special workarounds for lazy collections? how?
      3) what is a "best practice" for this kind of situation?

        • 1. Re: ejb3 best practice question
          mazz

          I think you will want to investigate JBoss SEAM. It probably will answer all your questions regarding how to do what you want to do.

          http://www.jboss.com/products/seam

          • 2. Re: ejb3 best practice question
            kh2ouija

            Investigating Seam, I now have another "how would you do it" question. I think it's silly, but anyway:

            So far, I've been using the GenericDAO pattern and I want to reuse it with Seam. My GenericDAOBean's EntityManager is not EXTENDED type, and my *DAOBeans are all Stateless; this has been OK so far. But in the Seam reference, some examples (like the MessageListBean from the tutorial) is stateful and uses an extended EntityManager; I suppose using a stateless DAO bean inside it instead of the EntityManager is therefore not an option. I could just make all the *DAOBeans stateful but I'm not sure it's the right thing to do.

            So, how can I reuse the DAO beans inside the Seam session beans?

            • 3. Re: ejb3 best practice question
              epbernard

              if your SFSB EXTENDED use a SLSB, the same persistence context is propagated, if I remember.

              • 4. Re: ejb3 best practice question
                kh2ouija

                Let me understand this...

                In the Seam tutorial, the MessageListBean is stateful and uses an extended EntityManager. Would it be OK to use stateless DAO beans instead of that EntityManager? (those DAOs use an EntityManager which isn't extended)

                • 5. Re: ejb3 best practice question
                  kh2ouija

                  that is, instead of

                  @PersistenceContext(type=EXTENDED)
                  private EntityManager em;
                  
                  @Factory("messages")
                  public void findMessages()
                  {
                   messages = em.createQuery("from Message msg order by msg.datetime desc").getResultList();
                  }
                  


                  I'd have

                  @EJB
                  private MessageDAO messageDAO;
                  
                  @Factory("messages")
                  public void findMessages()
                  {
                   messages = messageDAO.findAll();
                  }
                  



                  where the MessageDAOBean is stateless and uses a non-extended EntityManager.

                  Is this the same thing? do I have to make the MessageDAOBean stateful too?



                  • 6. Re: ejb3 best practice question
                    epbernard

                     

                    @PersistenceContext(type=EXTENDED)
                    private EntityManager em;
                    
                    @EJB
                    private MessageDAO messageDAO;
                    
                    @Factory("messages")
                    public void findMessages()
                    {
                     messages = messageDAO.findAll();
                    }
                    



                    • 7. Re: ejb3 best practice question
                      kh2ouija

                      Emmanuel, sorry but I don't understand your last message, can you please explain? I intend not to use the EntityManager in that bean, only the DAOs.