3

I have been reading about using Spring with Hibernate and I am really confused about session management. Hopefully someone can clear a few things up for me,

First of all I have no idea how sessions are managed when using HibernateTemplate. Is a session opened and closed when you call a method Eg Save() on the template? When you use the find() method, are detached objects returned?

I have read the Spring section on transactions but it mostly talks about handling exceptions. I was hoping to find some way of binding a hibernate session to a Spring transaction so that I can commit changes to hibernate objects when the transaction finishes. Is there a way to achieve this?

1 Answer 1

2

Spring manages the session for you. Looking in the documentation, specifically in section 13.3.1, you see

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
      <list>
        <value>product.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
  </bean>

and then in section 13.3.3 you see this

<bean id="transactionManager" 
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

If you look carefully, you will see that the session factory uses the datasource, and the transaction manager uses the sessionfactory, that way Spring could get your sessions for you, and wrap all your persistence code in a transaction.

Sign up to request clarification or add additional context in comments.

5 Comments

Its not very clear about what actually happens. If this transaction manager is used for a particular method does that mean that all database access inside that method will happen in the same hibernate session? If I load a hibernate object inside this method, will changes to it be committed when the method ends?
@toc the transaction manager is always being used. Depending on how you specify which methods to protect, it will do different things. The session automatically flushes before it is closed; hibernate manages that, not Spring. You should check out the opensessioninview filter.
@toc, if you want to know how it works, i would suggest you download the source and then use your debugger to walk through your code.
But it is Spring that opens and closes the session right? The reason I am asking is I would like to eliminate many of the save() and update() method calls from my code. At the moment I am using a Dao to get an object, then I modify it and then I call saveOrUpdate(). If the transaction manager created a session before entering the method and closed it when exiting I wouldn't need to call saveOrUpdate().
Personally I wouldn't worry about calling saveOrUpdate. The session will decide what to do when it closes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.