I gather that Hibernate uses the javassist library to handle lazy initialization of objects. Say I have the following entities (getters/setters/annotations are truncated):
@Entity
public class MainEntity {
private ComponentEntity comp;
}
@Entity
public class ComponentEntity {
private Integer id;
private String name;
}
Now I call the following method:
@Transactional
public void doSomething() {
MainEntity main = this.dao.find(1);
// Case A
main.getComp().getName();
// Case B
String localVariableName = main.getComp().getName();
}
When the DAO retrieves main, the comp object is not yet initialized due to lazy initialization. I expect that after calling Case A, the comp object will have been retrieved from the database, but based on the debugger, all the comp object properties appear as null.
It's only when after Case B where I save the name value into a localVariableName that I can see localVariableName get a non-null value.
Why is Eclipse showing my object properties as null?
Hibernate-managed objects that are lazily-initialized are managed by javassist proxy objects. Therefore in the Eclipse debugger, you have to know where to look.

The offer object is a proxy object that contains a handler object which contains a flag called initialized. It's currently set to false.

After Case A, the handler's initialized flag is now set to true. The handler.target object also changes to reflect the actual offer object's initialized properties.
So the lazy initialization is working as intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With