1 2 Previous Next 16 Replies Latest reply on Dec 19, 2006 2:08 PM by pgrillo

    Can Seam work with no ejb3 but with Hibernate ORM?

    pgrillo

      We are planning on using JSF in an upcoming project. We have chosen JSF, facelets, myfaces etc. I am now looking into Seam and possibly shale (or neither).

      It appears that Seam's session handling, use of annotations, and conversational options are very nice.

      However, for reasons i dont want to get into, i cannot use EJB3. Basically we have a fairly robust object model that supports persistance through Hibernate 3.0 and it's mapping files.

      Can we use Seam without EJB3 but with straight Hibernate ORM support? And more importantly, any thoughts on whether and how worthwhile it is without EJB?

        • 1. Re: Can Seam work with no ejb3 but with Hibernate ORM?

          Hi pgrillo

          It is very possible to use Seam with just hibernate (Seam is written by one of the original hibernate people). The only downside of not using the ejb3 stuff is the stateless and stateful session beans, but these probably bereplaced by java beans that do exactly the same functionality (minus some transactional stuff and clustering functionality).

          So my recommendation is to use Seam as it works very well ontop of hibernate.

          These are my thoughts for what there worth.

          • 2. Re: Can Seam work with no ejb3 but with Hibernate ORM?
            pgrillo

            I'd noticed this was possible, and tried to deploy a java beans only version of the booking example on tomcat.

            I'm a little confused on the transaction stuff that you allude to that only is supported using ejb session/entity beans.

            Seam Documentation states:

            9.4. Seam managed transactions
            ...
            Seam completely solves the problem of unwanted LazyInitializationExceptions, while working around the biggest problem in the open session in view pattern. The solution comes in two parts:
            ? use an extended persistence context that is scoped to the conversation, instead of to the request
            ? use two transactions per request; the first spans the beginning of the update model values phase until the end of the invoke application phase; the second spans the render response phase


            and

            9.5. Configuring Seam with Hibernate in Java EE
            ...

            Seam JavaBean components do not provide declarative transaction demarcation like session beans do. You could manage your transactions manually using the JTA UserTransaction (you could even implement your own declarative transaction management in a Seam interceptor). But most applications will use Seam managed transactions when using Hibernate with JavaBeans. Follow the instructions above to enable SeamExtendedManagedPersistencePhaseListener.


            But it is not clear to me if i am able or "not" able to solve the Lazy Initialization Exceptions and replace the "open session in view" pattern by using Seam "without" ejb.

            Can anybody verify, that i can solve the above problem using Seam managed transactions without ejb3?


            • 3. Re: Can Seam work with no ejb3 but with Hibernate ORM?
              gavin.king

              Have you looked at the "hibernate" example, it shows you how to do what you are trying to do.

              By the way, Seam 1.1 (CVS) has very much enhanced Seam's support for non-EJB3 environments. We are about to start testing on WebLogic, WebSphere, etc.

              • 4. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                gavin.king

                BTW, if you are already set on using JSF and Hibernate, you would be totally nuts to not use Seam ;-)

                • 5. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                  gavin.king

                   

                  But it is not clear to me if i am able or "not" able to solve the Lazy Initialization Exceptions and replace the "open session in view" pattern by using Seam "without" ejb.


                  Yes. You use Seam-managed transactions, instead of EJB method-level transaction demarcation.

                  • 6. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                    pgrillo

                    Sorry, i "thought" i was monitoring this thread, so missed the info.

                    Since then we have decided that Seam is an excellent way to go with our thin client implementation of our system. I'm trying to get my hands around whether and how we can integrate our current hibernate 3.2 based component into Seam, without having a dependency on Seam, since our component will also be used in other applications that are not thin client.

                    What we have:



                    Set of Pojo Business Objects, quite robust.
                    Set of DAOs pretty much consistent with Christian's approach in Java Peristence with Hibernate
                    DAOs use getCurrenSession
                    Currently use "hibernate.current_session_context_class=managed"
                    Currently using our own filterservlet, would move to Seam handling it.
                    HibernateFactory is built dynamically by our own Factory component, where the DAO can reference it
                    Use Java 5 and Tomcat 5.5.9, as well as weblogic.
                    We do not currently use JTA or EJB3, or even JNDI



                    So we now wish to use Seam. Our above DAOs would be readily available to any seam component that needs it, simply by accessing our Factory. Generally speaking, the session is not as interesting or important to our components as the DAOs. That is injecting the session might be nice, but injecting the session into a DAO makes DAO dependent on seam injection.

                    What i need is simply (or not so simply) to

                    have Seam manage the hibernate session in a way that it continues to be available to our DAOs, and also, if possible, to allow transaction demarcation when needed at least where Seam intercepts and manages the session


                    The excerpt below seems to show how to do this. However, it says "if we want a Seam managed Hibernate Session to be available" which is okay, but does this imply that doing the following also acheives my above goal? Hibernate manages the lifecycle of the session, and also provides access to the hibernate session to our DAOS (getCurrentSession?) "without" injection, since i do not want injection inside the DAOs?



                    9.5.2. Using a Seam-managed Hibernate Session

                    We will also need to configure a managed session if we want a Seam managed Hibernate Session to be available via injection.

                    To configure our Seam component, as usual, we use components.xml:
                    <component class="org.jboss.seam.core.Hibernate"/>
                    <component name="bookingDatabase"
                     class="org.jboss.seam.core.ManagedHibernateSession">
                     <property name="sessionFactoryJndiName">java:/bookingSessionFactory</property>
                    </component>
                    







                    • 7. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                      pgrillo

                      i would like to add underscore on other item. We are using Hibernate, not Java Persistence API implemented with Hibernate.

                      Not sure if that makes matters worth.

                      Just need to know if this combination is doable.

                      • 8. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                        gavin.king

                        It sounds like the problem you are trying to solve is:

                        "how can I have my DAOs use the Seam-managed persistence context when running inside the context of Seam, but use some other method of persistence context management outside of Seam"

                        And I guess you also want to be able to inject your DAOs into Seam components.

                        Probably the easiest way is to declare them in components.xml, and let Seam inject the Session that way:


                        <component name="customerDao" class="com.mydomain.myapp.CustomerDao">
                         <property name="session">#{mySession}</property>
                        </component>


                        Then your DAO's getSession() method can check if the session has been injected, and return that one if it has been, otherwise use your current mechanism to look up a session from Hibernate.

                        • 9. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                          pgrillo

                           

                          "how can I have my DAOs use the Seam-managed persistence context when running inside the context of Seam, but use some other method of persistence context management outside of Seam"


                          Yes, that is my main problem. However, our DAOs are not publicly constructable, they are returned from one of our DAO factories, given a "product id" because we have different flavors of "Customer DAOs" depending on the context (productID) in which we are working. It's the nature of our business.

                          Our code knows its productID, and simply does:

                          DOAFactory.getFactory(productID).getCustomerDAO()

                          However, our DAO's DO have setSession(Session) on them as well (override).

                          So, maybe there is an answer somewhere using your approach, i've just got to figure it out. perhaps our seam components don't use our DAOFactory directly, but a Seam component that interacts with our DAOFactory, that will get the DAO and set the "injected" session into it ....

                          Thanks for the idea, Gavin, i'll see if i can get it working.




                          • 10. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                            gavin.king

                             

                            <component name="customerDao" class="com.mydomain.myapp.CustomerDao">
                             <property name="session">#{mySession}</property>
                             <property name="productId">#{product.id}</property>
                            </component>


                            Or, if that still does not work for you, use the "manager component pattern", ie. @Unwrap

                            http://docs.jboss.com/seam/1.1GA/reference/en/html/concepts.html#d0e2884

                            • 11. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                              gavin.king

                              Note that if by "different flavor", you really mean a different class, then the manager or factory component is definitely the way to go. (Manager component is the generic pattern for integrating things into the Seam environment that otherwise do not quite "fit".

                              • 12. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                                pgrillo

                                Yes, different flavor means different class. Multiple factories by productID may return specializations of any of our DAOs.

                                Sorry, for this last question. Just trying to inject my Hibernate session, and the doc reads.

                                Seam will bootstrap a Hibernate SessionFactory from your hibernate.cfg.xml file if you install the built-in
                                component named org.jboss.seam.core.hibernate.


                                Does this mean that my Hibernate session must be fully defined through the hibernate.cfg.xml?

                                Because, I build the Hibernate Factory dynamically. I start with a hibernate properties file, then dynamically find optional mapping files that may or may not exist, and dynamically add them, then build it.

                                So, everything is not self contained or static in a configuration file. Is there a way to somehow provide Seam with the Factory that our app built? Or have i misread what i required to get Seam a reference to our factory so it can manage our session.

                                • 13. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                                  gavin.king

                                   

                                  Is there a way to somehow provide Seam with the Factory that our app built?


                                  Sure, Seam has some built-in component that manages a Hibernate SessionFactory, but absolutely nothing forces you to use this (simple) class. There is no problem at all to write you own. Just use org.jboss.seam.core.HibernateSessionFactory for inspiration.

                                  Seam will bootstrap a Hibernate SessionFactory from your hibernate.cfg.xml file if you install the built-in
                                  component named org.jboss.seam.core.hibernate.


                                  Ahem, this comment is actually obsolete, I'll fix the docs.

                                  • 14. Re: Can Seam work with no ejb3 but with Hibernate ORM?
                                    pgrillo

                                    So, i guess maybe i missed something obvious.

                                    Is it true that support of extended persistence context requires that our non JPA (Hibernate 3 only) support must utilize JTA? We were using only JDBC and Hibernate Transaction thus far.


                                    If so, does it follow, that i'll need the jboss microcontainer to provide all that and i'll need to change the way i load/build my session factory using JTA, etc.

                                    As mentioned, we build our session factory on the fly as we look for particular mapping files to add to the configuration before we actually call the build.



                                    1 2 Previous Next