Announcement Announcement Module
No announcement yet.
Hibernate and lazy load Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate and lazy load

    Sorry about the repost, I posted to wrong forum previously.


    Here is my situation. I have three classes A, B, and C. Class A has a collection of B and B has a collection of C. I'm using Hibernate for persistence. I have mapped the collections as lazy - since most of them I don't need to retrieve the entire graph. I have DAO classes for each A, B, and C. Each DAO classes extends HibernateDaoSupport.

    My problem is, for one case I need to retrieve the entire graph. After rereading the the docs and the forums, I realize that HibernateDaoSupport opens and closes the session for me and to avoid a LazyInitializationException I need to reattach the object to a session. Fair enough.

    My question, is would it be a good idea to create an Intercepter on A.getB() and B.getC() that would make a call to Hibernate.initialize() ?

    If not, or if there is a better idea, could someone direct me to sample code? I've looked at the Spring samples but there does not appear to be an example of using Hibernate with lazy collections.


  • #2
    You could explicitly touch the relationship methods within your DAO or the enclosing transaction from service layer. Or you could use a fetch join. I don't much like the interceptor approach: seems a bit too much magic vs these simpler techniques.


    • #3
      did you get this figured out?

      I'm a new user to everything, having a similar issue with A has collection of B, B has collection of C, and getting the lazy loading to work when you want to eagerly fetch in one instance. I'm not sure how to initialize the collections using the HibernateDAOSupport getHibernateTemplate calls. I originally do a find, then I want to initialize the collections associtated to the object I found. Any help would be appreciated, I have read the documentation and I am still lost. Thanks.