Results 1 to 5 of 5

Thread: Object identity with HibernateTemplate

  1. #1

    Default Object identity with HibernateTemplate

    Hi!
    I am using Spring with Hibernate, and i am confused by the following (taken from a junit test):

    Code:
    Subject subject1a = (Subject) getHibernateTemplate().find("FROM Subject s WHERE s.title='Subject 1'").get(0);
    Subject subject1b = (Subject) getHibernateTemplate().find("FROM Subject s WHERE s.title='Subject 1'").get(0);
    
    // the following fails!
    assertTrues(subject1a==subject1b);
    I read the Hibernate manual on Identity/Equals, and i thought that the two find's should return exactly the same object (in terms of Java-VM identity). Am i wrong about that?

    I tried the same with a "plain" Hibernate session:
    Code:
    Subject subject1a = (Subject) hibernateSession.find("FROM Subject s WHERE s.title='Subject 1'").get(0);
    Subject subject1b = (Subject) hibernateSession.find("FROM Subject s WHERE s.title='Subject 1'").get(0);
    
    // the following is true!
    assertTrue(subject1a==subject1b);
    And this works. I thought HibernateTemplate would store the HibernateSession in a thread bound variable and reuse it on the next call. But apparently in the first code snippet a new session is opened for every find-call.

    Did i miss something important?

    I will post more information about my setup below.

    Thanks for your help!
    Sebastian

  2. #2

    Default

    BTW: I also tried this identity comparison in a controller, with my (otherwise working) Managers and DAOs. The comparison failed there, too...

    In the JUnit test i setUp the following:
    Code:
    sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    sessionFactory.afterPropertiesSet();
    
    hibTempl = new HibernateTemplate((SessionFactory) sessionFactory.getObject());
    (the method getHibernateTemplate() in my JUnit test simply returns hibTempl)

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Spring will only do the ThreadLocal binding in the context of a single transaction. You need to create a transaction in your tests, using Spring's declarative tx management or programmatically with the HibernateTransactionManager. I typically use an abstract integration test superclass that creates a transaction in setUp() and rolls it back in tearDown().

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    This is why I personally also always set up my HibernateTemplates with the allowCreate flag set to off (session must already exist, presumably set up alongside the transaction by HibernateTransactionManager or another transaction manager+HibernateInterceptor).

    With the flag set to the default true, it's too easy to run code out of a transaction, and accidentally create a session when you don't realize it...

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  5. #5

    Default

    Hi Rod, Colin!

    Thanks for your replies! This solved my problem... :-)

    Sebastian

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •