-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problems with Lazy Loading - Lazy Getters Return Nulls
PostPosted: Mon Feb 28, 2011 10:48 am 
Newbie

Joined: Thu Aug 20, 2009 7:20 am
Posts: 5
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.


Top
 Profile  
 
 Post subject: Re: Problems with Lazy Loading - Lazy Getters Return Nulls
PostPosted: Mon Feb 28, 2011 12:11 pm 
Newbie

Joined: Thu Aug 20, 2009 7:20 am
Posts: 5
Just a snippet of some representative code in case it helps.

An Employee can be assigned to a Position and a Position can be occupied by only one Employee. A Position is associated with a JobCode (which has an attribute for JobTitle). An Employee is managed by one other Employee, but can manage multiple Employee's. I am including only the association mappings here, not all annotations.

If the fetch type for currentPosition, managerEmployee, and jobCode are LAZY we see the issue; if they are EAGER we do not. Anybody see anything problematic about this mapping?

public class Employee {

...

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CURRENT_POSITION_ID")
private Position currentPosition;

...

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MANAGER_EMPLOYEE_ID")
private Employee managerEmployee;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "managerEmployee")
@BatchSize(size = CraftEntity.DEFAULT_BATCH_SIZE)
private List<Employee> subordinates = new ArrayList<Employee>();

...

}

public class Position {

...

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "JOB_CDE")
private JobCode jobCode;

...

@Valid
@OneToOne(fetch = FetchType.EAGER, mappedBy = "currentPosition", cascade = CascadeType.ALL)
@BatchSize(size = CraftEntity.DEFAULT_BATCH_SIZE)
private Employee employee;

...

}


Top
 Profile  
 
 Post subject: Re: Problems with Lazy Loading - Lazy Getters Return Nulls
PostPosted: Mon Feb 28, 2011 12:14 pm 
Newbie

Joined: Thu Aug 20, 2009 7:20 am
Posts: 5
A bit more clarification about what I mean by 'works' and 'not works'.

With LAZY, when subsequent to the initial query I try the following ...

employee.getManagerEmployee().getFirstName() // returns null
employee.getCurrentPosition().getJobCode().getJobTitle() // returns null

If I set fetch type to EAGER, both of these return valid data for the exact same object returned by the same query.


Top
 Profile  
 
 Post subject: Re: Problems with Lazy Loading - Lazy Getters Return Nulls
PostPosted: Wed Jul 20, 2011 1:44 pm 
Newbie

Joined: Wed Jul 20, 2011 1:40 pm
Posts: 1
Have you heard any suggestion for this issue? Or have you solved this issue?
Thanks in advanced.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.