[hibernate-issues] [Hibernate-JIRA] Created: (HHH-3951) Loader.doQueryAndInitializeNonLazyCollections() does not anticipate the case that the collection is accessed in the setter

Michael Papadopoulos (JIRA) noreply at atlassian.com
Wed Jun 10 08:50:13 EDT 2009


Loader.doQueryAndInitializeNonLazyCollections() does not anticipate the case that the collection is accessed in the setter
--------------------------------------------------------------------------------------------------------------------------

                 Key: HHH-3951
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3951
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.3.1
            Reporter: Michael Papadopoulos


Assume that entity Foo owns a non-lazy set of instances of entity Bar. The corresponding setter looks like this:

class Foo {
....
    
    void setBars( Set<Bar> bars) {
        this.bars = bars;

        if (bars != null)  {
            // 'capacity' is a calculated value
            this.capacity = bars.size();
         }
    }
....
}

Since property 'bars' is non-lazy, one would assume that it's perfectly ok to access it within the setter, for example in order to perform a calculation based on the property that has just been set. This, however, results in an exception. The problem is that when Hibernate loads Foo, it first calls setBars() with a lazy collection, and then, after the setter has returned, initializes this collection. So, accessing the collection in the setter results in a LazyInitializationException with the message: "illegal access to loading collection". Consider method doQueryAndInitializeNonLazyCollections() in org.hibernate.loader.Loader:

	private List doQueryAndInitializeNonLazyCollections(....)  ... {

		final PersistenceContext persistenceContext = session.getPersistenceContext();
		persistenceContext.beforeLoad();
		List result;
		try {
			result = doQuery( session, queryParameters, returnProxies );
		}
		finally {
			persistenceContext.afterLoad();
		}
		persistenceContext.initializeNonLazyCollections();
		return result;
	}

doQuery() will call the setter with a lazy collection in 'initializing' status. Accessing a lazy collection that is in 'initializing' status results in the aforementioned LazyInitializationException. I think the order in which Hibernate is doing things here is incorrect. It should first make sure the collection is fully initialized before passing it to the setter. Otherwise it is violating setter expectations/semantics. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list