Hibernate version: 3.2.4sp1
I've found a similar bug on JIRA (bug HB-12) that is supposed to be fixed, but I still encounter the same [similar] problem:
http://opensource.atlassian.com/project ... owse/HB-12
I'm using the Criteria API to retrieve the events along with the device info. Since I'm using fetch="join", Hibernate also loads the parent, which is fine. However, as described in bug HB-12, if the parent is null (it's optional after all), an extra load is performed.
Although this is not a n+1 select problem, it could be one if none of the parents exists.
I've tracked down the problem to the 2-phase loading mechanism:
CriteriaLoader.initializeEntitiesAndCollections()
--> TwoPhaseLoad.initializeEntity()
--> ManyToOneType(EntityType).resolve()
...
finally, the session dispatch the Load event, which causes the LoadEventListener to try to load the parent entity from the session cache. If it was not in the cache (was null), then a separate load will be performed. The extra load is useless because it'd still get a null parent.
I'd expect Hibernate to remember that the parent was null in the initial select and ignore the 2nd load.
Is this a bug? Is there a solution/workaround?