5 Replies Latest reply on Aug 13, 2006 1:46 PM by None None

    Displaying EJBs within JavaScript: what about the EntityMana

    None None Master

      Here's the scenario: I have a Servlet which uses JNDI to get a stateless session bean. That stateless session bean has a method like "load" which loads up one of my entities.

      So now my Servlet has gotten a hold of its entity bean. Let's say this entity is an Invoice which contains a List of Items.

      Then the Servlet puts that Invoice object into the request as an attribute, and uses JSP to do the display. The JSP now iterates through the List of Items.

      Here's the problem: What if that List of Items hasn't been initialized? That List might be just a stub. It needs to get all its Items, and then it needs to get values out of the Items (like price, description, etc). But by now, there is no more entity manager.

      How can these proxies be resolved in a situation like this? What is the way to handle this?

      It seems like a common problem. I know that back when I was using Hibernate and plain old Tomcat (not within JBoss), I set up a Filter for everything. The Filter would create a Hibernate Session, load the objects it needed, call doFilter() to actually display everything, and then call session.close() so the session wouldn't be left dangling.

      What's the right way to do this within JBoss? I have a feeling that the answer might be "use Seam", but I don't want to do that at this stage, because a) I have a lot of existing JSPs that I can't rewrite and b) I'm still getting myself comfortable with JBoss / EJB and I don't want to add in yet another technology I need to learn before I can demo this project to customers.

      Any thoughts on this?

      By the way, using EJBs within JBoss is quite amazing. I set up my classes, I deploy them, all the tables magically appear, and it is just cool. So this question is the last barrier I have to being able to use EJBs + JBoss as an effective tool.

      Thanks

        • 1. Re: Displaying EJBs within JavaScript: what about the Entity
          None None Master

          Obviously, the title should be "JSP", not "JavaScript".

          • 2. Re: Displaying EJBs within JavaScript: what about the Entity
            None None Master

            Any ideas on this? I've looked around the net and seen that I'm obviously not the only one who is encountering this issue. You grab your EJBs somehow while in a JSF backing bean, and then when JSP tries to display them, it's an error because they are detached and some members are not initialized.

            Solution 1: Use a Filter that keeps an EntityManager hanging around for the duration of the request. Seems like an ugly way to do it, but I guess that would be "PHP-style", where it automatically frees DB connections when the request is finished.

            Solution 2: Use Seam. Looks like a lot of people like that idea. I'm thinking about doing it, but I need to get this application in demo soon, and I'm not sure if I want to take on yet another layer of complexity here. On the plus side, Seam looks quite interesting. Is this the way to do it?

            Solution 3: Anything else?

            This is a problem that would be universal in any type of JSP + EJB application, so I'm surprised at how hard it is to find answers on this. Please let me know if you have ideas.

            • 3. Re: Displaying EJBs within JavaScript: what about the Entity
              None None Master

              By the way, it seems like ideally this is something that would be handled within the Servlet container itself. You should be able to register some kind of listener for the creation of a Request object and then another for when it is finished, which should be able to handle things like attaching and detaching an entity manager.

              • 4. Re: Displaying EJBs within JavaScript: what about the Entity
                Raja Master

                 

                "SmokingAPipe" wrote:

                Solution 1: Use a Filter that keeps an EntityManager hanging around for the duration of the request. Seems like an ugly way to do it, but I guess that would be "PHP-style", where it automatically frees DB connections when the request is finished.


                This is how the OpenSessionInView works and I believe is the only way available if you dont want to get to Seam. Check this post
                http://www.jboss.com/index.html?module=bb&op=viewtopic&t=88460
                for some differences in Seam managed persistence context Vs. EJB3's Extended Persistence context that will make applications not worry about LazyInitExceptions.


                • 5. Re: Displaying EJBs within JavaScript: what about the Entity
                  None None Master

                  Thanks Raja. This problem seems quite absurd, because there's not much use to EJBs if there isn't an easy and reliable way to render them in JSP. There really should be some part of the Request object that makes it possible to manage request-scoped resources.

                  I think I'll take the plunge and do this with Seam. It looks like it was really designed to do all this stuff. I'm not thrilled to have to learn yet another framework when I have a deadline to meet, but either I move forward (Seam) or backwards (PHP).