4 Replies Latest reply on May 2, 2005 7:11 AM by Kabir Khan

    Using EJB3 in jsps - please help

    Brian Newbie

      Hi all,

      I've been a devout user of jboss for several years. I've been in a startup that went bust, in part due to the horrendous amount of time and complexity it took to develop ejbs in the past. That's why I think ejb3 looks so good.

      Now, I'm developing an application which uses ejb3. I have jsps which call struts actions which call ejb3 session beans. Now previously, I used to use Data Transfer Objects between these tiers rather than passing entity beans to the front end (jsps). But this really means you have to code your object relational mapping twice - very nasty really. I want to know if you can actually pass the ejb3s back to the actions and use them directly in jsps.

      Can someone tell me if using ejb3 entity beans in the jsps is ok? I've read some stuff about attachment/detachment before, but I'm not sure what this is about, perhaps it's relavent.

      thanks very much,
      Brian

        • 1. Re: Using EJB3 in jsps - please help
          Emmanuel Bernard Master

          Absolutly, you can use EJB3 entities in your jsps/action classes.
          What you be aware of is to fetch all your needed proeprties/associations before sending the entity back to the upper layers.
          detach/attach is used to update some objects (entities) in your upper layer (presentation) and synchronize them to your DB through a merge in your entity manager.

          • 2. Re: Using EJB3 in jsps - please help
            Kabir Khan Master

            Yes, you can use the entity beans as dto's.

            Once passed out of the EJB container to the web container the entities are no longer attached to the database. This means that you for example cannot lazy load relationships from the web layer, but need to make sure your session bean layer eager loads any data that will be needed. If you change the data of an entity in the web layer, you can pass it back to the session layer and update the data in the database using EntityManager.merge()

            • 3. Re: Using EJB3 in jsps - please help
              Brian Newbie

              Thanks guys!
              This is excellent! You've just saved me LOADS of coding! Really appreciate it! Just to confirm one thing - the attachment/reattachment is handled seamlessly as far as I am concerned correct?

              Once the entity manager passes the ejb3 object back to me from a session bean, it's detached, and until I call a session bean method something like

              public void update(Object myDetachedObject){
              entityManager.merge(myDetachedObject);
              }

              it doesn't get reattached, right?

              thanks a million,
              Brian