Enabling DEBUG or lower level on org.hibernate.orm.results.loading caused the toString() method of my entity class to throw NullPointerException (NPE) in Hibernate 6.1.7. Here is the snippet that caused the NPE from my logback-test.xml file.
Here is the toString() method of my PcccFrame entity class, which throws NPE:
Note that my pccc field in the PcccFrame entity is a required (nullable = false) @ManyToOne reference property and its naturalKey is a required String property (you may just ignore drugName field; it’s not relevant). In other words, my PcccFrame object can never have a dangling pccc reference in the database. I am relying on Hibernate to eager-load pccc for me. I got a NullPointerException when I try to access a collection of PcccFrame objects from a pccc instance. The (partial) stack trace is as follows:
Our code has been like this since Hibernate 5 and it hasn’t changed. We recently upgraded to Hibernate 6 and began to see this kind of NPE when debugging. Peeking at the source code of AbstractEntityInitializer.java (line 708 as of hibernate-core version 6.1.7.Final), it seems the debug print shouldn’t be too aggressively assuming that the Hibernate-managed reference is already loaded. Here is the instantiateEntity method of AbstractEntityInitializer that I peeked at:
@beikov identified it as a bug. At this point, we work around it by setting the log level to INFO or above. |