Sunday, June 22, 2008

Extra-lazy collection fetching in Hibernate

I came across some new functionality in Hibernate recently, extra-lazy collection fetching. This functionality, which seems oddly named, basically allows you to initialize individual list or map items, if you have everything configured in Hibernate just right. The details of my spike solutions are written up on my wiki:

For small lists, I would recommend not using this functionality, as every item is a SQL call to the database. For a large list or child objects which are expensive to create, this might be just what you're looking for. It took me a while to get the list semantics to work properly. The @IndexedCollection is very important. I have not played with a map collection yet, but in theory that collection should work similarly.


  1. hello Bart

    i've read your post and code

    and i have a doubt:
    In a MAnyToMany assoc:

    if I have to add a element to a collection and I'm using the @Lazy Extra feature, my code will look like:


    Will hibernate load all the collection?

    if yes this is a big problem if I hava 1 million objects in that collection...

    thank you for your example


  2. At least for the OneToMany relationship, Hibernate does not load the elements of the collection when adding new elements. I've updated the wiki write up with a JUnit test case and Hibernate generated SQL demonstrating that extra-lazy collection fetching will ensure that elements are not faulted in when adding a new element (in my case, a Wine to a Winery).

  3. Hi Bart,

    reading your last example (adding an element to an extra-lazy collection) I wonder where Hibernate determines the list_index of the added element. The SQL trace does not show a statement that returns the maximum index used.


  4. What exactly is the difference among these three things. I am confused with explanation given on Hibernate website.


    (specially i want to understand this)