We are using Hibernate3 with JPA Annotations and have defined many of our associations as LAZY. Much of the time, LAZY associations work as expected; we load the class and when we call the getter for the association object, the correct object is returned. However, for a number of associations we are seeing unpredictable behavior; we fetch a collection of parent objects and as we iterate over them and call the LAZY getters, sometimes they return the correct object and other times they return NULL. In some cases the same specific object instance shows up in different lists (based on the criteria used to select the list) and its behavior is not consistent; when it appears in one list, the getter returns the correct value but when it appears in a different list it returns NULL. Stranger still, sometimes the first time we call the getter on a specific instance it returns NULL, but if we call it again on the same object instance it returns the correct object.
I have verified that the query of the original list and the subsequent accesses of the LAZY associations are occurring within the same transaction and the session is not being closed between the initial fetch and the LAZY accesses. Also, we do not ever get a LazyInitializationException. We get no exceptions at all, just null values from the getters.
I have also verified that neither the entity classes nor any of their methods are declared final (though I think if that were the case, none of the getters would ever return anything but NULL).
Without changing anything else, I changed the mapping for the associations in question to EAGER and everything works.
Also, in at least one of the cases we tried inserting a JOIN FETCH while leaving the mapping as LAZY and it also worked.
Is this a bug? I've searched and I was unable to find any other reports that sounded like the behavior we're seeing.
Can anybody suggest any other thing we could be doing incorrectly that would result in such erratic behavior?
I even started to wonder if maybe we are using some incompatible versions of some jar files or something like that. I have listed the versions of the assorted Hibernate jars that we currently have in our classpath just in case.
hibernate-core-3.3.2.GA.jar hibernate-entitymanager-3.4.0.GA.jar ejb3-persistence-1.0.2.GA.jar hibernate-commons-annotations-3.1.0.GA.jar hibernate-annontations-3.4.0.GA.jar hibernate-ehcache-3.3.2.GA.jar cglib-2.2.jar
Thanks.
|