7 Replies Latest reply on Jul 28, 2006 9:17 PM by gavin.king

    Mixing Hibernate & EJB3

    mschmidke

      Hello,

      I would like to directly access the Hibernate behind JBoss' EJB3 implementation. Usually, this is no problem since I can inject from a PersistenceContext into a Hibernate Session Object:

      @PersistenceContext(unitName = "LeistungserfassungEntityManager")
      protected Session leDB;
      


      Now I want to migrate to Seam-managed Hibernate Session (inside EJB3!), but I don't understand how to adapt chapters 9.4.2 / 9.5.2 to get this running.

      Is this possible? Can someone give me an example of how to configure it?

      Regards,

      Marcus.

        • 1. Re: Mixing Hibernate & EJB3
          gavin.king

          Yes, of course it is possible, straightforward in fact. Just

          (1) install a Seam-managed session in components.xml
          (2) inject it using @In(create=true) mySessionComponentName

          • 2. Re: Mixing Hibernate & EJB3
            mschmidke

            Yes ... but which sessionFactoryJndiName do I have to use in components.xml? Where do I have to configure the "counterpart" (since, of course, I don't have a hibernate.cfg.xml file)?

            Perhaps ... I'll try leaving out the sessionFactoryJndiName property ... maybe there is an automatic default?


            Marcus.

            • 3. Re: Mixing Hibernate & EJB3
              gavin.king

              You _will_ need a hibernate.cfg.xml, of course!

              • 4. Re: Mixing Hibernate & EJB3
                mschmidke

                 

                "gavin king" wrote:
                You _will_ need a hibernate.cfg.xml, of course!


                Will I? But ... until now, I never needed one. persistence.xml was all I had.

                Ok, when I create one, will I only have to put this session-factory entry into it? Or will I have to move configuration parameters from persistence.xml into hibernate.cfg.xml?

                persistence.xml:
                <?xml version="1.0" encoding="UTF-8"?>
                
                <persistence>
                 <persistence-unit name="LeistungserfassungEntityManager">
                 <jta-data-source>java:/LeistungserfassungDS</jta-data-source>
                 <properties>
                 <property name="hibernate.hbm2ddl.auto" value=""/>
                 <property name="hibernate.show_sql" value="true"/>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
                 <property name="hibernate.max_fetch_depth" value="0" />
                 <property name="hibernate.default_batch_fetch_size" value="100" />
                 <property name="hibernate.use_sql_comments" value="true" />
                 <property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
                 </properties>
                 </persistence-unit>
                </persistence>
                



                • 5. Re: Mixing Hibernate & EJB3
                  robjellinghaus

                  Personally I am doing it this way:

                  /**
                   * Tiny sugar class for getting the hibernate session from an EntityManager.
                   */
                  public class GetHibernateSession {
                   // wow, cool idiom!
                   private static final Logger log = Logger.getLogger(new Throwable().getStackTrace()[0].getClass());
                  
                   private GetHibernateSession () { }
                  
                   public static Session get (EntityManager entityManager) {
                   Session delegate = (Session)entityManager.getDelegate();
                   return delegate;
                   }
                  }

                  Use like so:
                  GetHibernateSession.get(entityManager).enableFilter("priorChangeset")
                   .setParameter("changeset_id", changeset.getId());

                  Works for me :-) And no need for hibernate.cfg.xml when I already have a persistence.xml....

                  Cheers!
                  Rob

                  • 6. Re: Mixing Hibernate & EJB3
                    mschmidke

                    Great ...

                    Did not know that Hibernate Session is that simply available.

                    Works great! This is the right solution for such small-brained people like me.

                    Thank you very much!

                    Marcus.

                    • 7. Re: Mixing Hibernate & EJB3
                      gavin.king

                       

                      Personally I am doing it this way:


                      Even better, do that stuff in an @Unwrap method on a Seam component, and you can inject the wrapped Hibernate session using @In.