Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse debugger shows Hibernate-managed object has not been lazily-initialized

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?

like image 346
Jensen Ching Avatar asked Nov 24 '25 02:11

Jensen Ching


1 Answers

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.

screenshot of null proxy object with handler.initialized set to false

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

screenshot of proxy object with null props but with hander.initialized set to true and handler.target containing the initialized actual object

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.

like image 198
Jensen Ching Avatar answered Nov 25 '25 15:11

Jensen Ching



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!