Announcement Announcement Module
Collapse
No announcement yet.
Hibernate properties trouble w/no persistence.xml & LCEMFB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate properties trouble w/no persistence.xml & LCEMFB

    Hi all,

    Sorry if this is a FAQ, but I can't seem to google the answer.

    Env: Spring 3.1.1.RELEASE, Spring Data JPA 1.1.0.RC1, Hibernate 3.6.10.Final

    Motivation: Migrating evolutionarily from pure Hibernate to JPA w/Hibernate as impl. Some components still need SessionFactory & Session instances during migration.

    If I'm using a LocalContainerEntityManagerFactoryBean along with Hibernate and I'm NOT using a META-INF/persistence.xml file, what's the correct incantation of Hibernate properties in order to get the following to return non-null?
    Code:
    public Session getCurrentSession() {
      EntityManagerFactory emf = beanFactory.getBean(EntityManagerFactory.class);
      SessionFactory sf = ((HibernateEntityManagerFactory)emf).getSessionFactory();
      return sf.getCurrentSession(); // returning null! :(
    }
    In particular, I'm looking for values I have to set for the following:
    • hibernate.current_session_context_class
    • hibernate.transaction.factory_class
    • hibernate.transaction.manager_lookup_class
    I can see in my logs, with none of these properties set, that a Session is getting created,
    Code:
    03/20/12 22:00:12:724: [] [] [SpringOsgiExtenderThread-37]: INFO  com.ea.nucleus.eaid.impl.group.GroupServiceImpl - Initializing group service impl.
    03/20/12 22:00:12:735: [] [] [SpringOsgiExtenderThread-37]: INFO  com.ea.nucleus.eaid.impl.group.GroupServiceImpl - Loading all groups and creating group map.
    03/20/12 22:00:12:740: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
    03/20/12 22:00:12:740: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
    03/20/12 22:00:12:921: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 5457022209167360
    03/20/12 22:00:12:921: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 5457022209167360
    03/20/12 22:00:12:926: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@5f297ee0] for JPA transaction
    03/20/12 22:00:12:926: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@5f297ee0] for JPA transaction
    03/20/12 22:00:12:947: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - begin
    03/20/12 22:00:12:947: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - begin
    03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
    03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
    03/20/12 22:00:13:030: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565d1ac3]
    03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
    03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
    03/20/12 22:00:13:030: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565d1ac3]
    03/20/12 22:00:13:060: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.transaction.support.TransactionTemplate - Initiating transaction rollback on application exception
    org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No CurrentSessionContext configured!
            at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:468)
    but getCurrentSession() is returning null, suggesting to me that I'm missing some plumbing setup between Spring & Hibernate.

    What does a dude gotta do to get this to work?

    TIA,
    Matthew

  • #2
    In particular, I'm looking for values I have to set for the following:
    hibernate.current_session_context_class
    hibernate.transaction.factory_class
    hibernate.transaction.manager_lookup_class
    Nothing, they shouldn't be there. Setting or changing these values will break spring integration. Also you shouldn't be using this way to get the session, you should use the current entitymanager and get its delegate which is the current session.

    Also you shouldn't be using HibernateTemplate as that should be considered deprecated (as well as the JpaTemplate!).

    Next to that you probably want to use the HibernateJpaSessionFactoryBean to get the session factory instead of implementing your own lookup mechanism.
    Last edited by Marten Deinum; Mar 21st, 2012, 02:19 AM.

    Comment


    • #3
      [solved]

      Originally posted by Marten Deinum View Post
      Nothing, they shouldn't be there. Setting or changing these values will break spring integration.
      Ok.
      Originally posted by Marten Deinum View Post
      Also you shouldn't be using this way to get the session, you should use the current entitymanager and get its delegate which is the current session.

      Also you shouldn't be using HibernateTemplate as that should be considered deprecated (as well as the JpaTemplate!).

      Next to that you probably want to use the HibernateJpaSessionFactoryBean to get the session factory instead of implementing your own lookup mechanism.
      I agree with your recommendations here, but, as I mentioned in my motivation above, I'm trying to slowly evolve a large codebase from Hibernate to JPA; I can't do it all at once. Many of the legacy legacy DAOs use HibernateTemplate.

      I didn't know about HibernateJpaSessionFactoryBean, so I replaced my own bean (which my code sample came from) with it. Unfortunately, I'm still got the same error. I can see why, too: in Hibernate's SessionFactoryImpl constructor, the currentSessionContext is getting set to null because buildCurrentSessionContext() is returning null.

      I know you said that none of the Hibernate properties I mentioned above need to be set, but while looking at buildCurrentSessionContext() again, I checked to see if Spring implemented Hibernate's CurrentSessionContext, and, of course, it does: org.springframework.orm.hibernate3.SpringSessionCo ntext. I set
      Code:
      hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext
      and things appear to be working ok.

      I think SpringSessionContext was the missing plumbing.

      Thanks,
      Matthew

      Comment


      • #4
        Spring normally sets the current session context itself (when it is null) so when people start messing around with hibernate.current_session_context_class transaction management breaks (unless you use JTA then you should change it). The class you mention is (or at least should) be set when spring comes into play.

        I would strongly advice to make it a priority to remove HibernateTemplate as that would also eliminate this hack (because IMHO that is what this is) and you should simple use the EntityManager to get the delegate (not the EntityManagerFactory).

        Comment


        • #5
          Understood. In this iteration, the goal is to get a thin vertical slice going from top to bottom with JPA. The next iteration is where we distribute the development load to the rest of the team to tackle the hundred or so legacy DAOs, replacing them with Spring Data JPA-based implementations. During that iteration, all references to not only HibernateTemplate, but also Hibernate, will be removed.

          Thanks,
          Matthew

          Comment


          • #6
            Originally posted by Marten Deinum View Post
            Spring normally sets the current session context itself (when it is null) so when people start messing around with hibernate.current_session_context_class transaction management breaks (unless you use JTA then you should change it). The class you mention is (or at least should) be set when spring comes into play.
            Just to clarify: Yes, spring normally sets the current session context itself when using spring's LocalSessionFactoryBean, which I am not using. I'm going the pure JPA route by using LCEMFB (LocalContainerEntityManagerFactoryBean) and setting its persistenceProviderClass property to org.hibernate.ejb.HibernatePersistence, which does not set Hibernate's hibernate.current_session_context_class AFAICT.

            -matthew

            Comment

            Working...
            X