Announcement Announcement Module
Collapse
No announcement yet.
How to do lazy loading with HibernateTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to do lazy loading with HibernateTemplate

    I have a table A which contains a collection of table B (one-to-many from A to B). Now in my mapping file for A, I specify lazy="true". After I load the A into an object, I want to retrieve all the B s associated with A. So I have to call (A)getHibernateTemplate().get(A.class, id) to retrieve an object of A for a specific id, but at this point, how do I retrieve all the Bs? The hibernate session is closed now. I don't want to write another query to retrieve Bs for A's id. How do I call A's getter method to retrieve all the Bs? Thanks for the help.

  • #2
    The trick is to place the call to objectA.getAllBs() in code that executes within a transaction. There are several ways to do this, but the most straightforward (in my opinion) is to use a TransactionProxyFactoryBean to generate a transaction proxy for all of the methods in an interface.

    The HibernateTemplate automatically participates in the transaction.

    There is quite a bit of discussion of this in the Spring reference doc, as well as tutorials from various sources. I suggest you have a look.

    Comment


    • #3
      Thanks for the reply. I think I found a better way to do it myself. I don't think it is good to put it into a transaction for read operations. The trick is to use OpenSessionInViewFilter or OpenSessionInViewInterceptor from Spring. Look at this URL:

      http://www.jroller.com/page/kbaum/Weblog

      Comment

      Working...
      X