org.hibernate.AssertionFailure: null identifierI hate Hibernate with a passion!
More in general, I tend to hate ORM pretty much, because I feel like I need to adapt my applications and code to what the ORM thinks it should be. However, I have to use some ORMs, most notably Hibernate (I’m not reporting the version because it is awkwardly old).
Anyway, yesterday I start seeing this strange exception on one of applications of mine:
Searching the web did not provide me any hint about, and that is the reason why I’m writing this post.
ERROR AssertionFailure <init> - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: null identifier at org.hibernate.engine.EntityKey.<init>(EntityKey.java:61) at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:688)
Trying to identify the problemMy first try was to enable as much logging as I could:
but that does not revelead nothing interesting, since the problem wa not in a query rather in the query result. I then turned off the above variables and let Hibernate to log at a
DEBUGlevel. That was enlightning because it revealed me a line like the following:
Wait a minute: what does
DEBUG StatefulPersistenceContext initializeNonLazyCollections - initializing non-lazy collections
initialize non-lazy collectionsmeans?
I did a code scan on the entities just to find out I’ve got a collection
@ManyToOnewith a fecthing type of
EAGER, and most notably the value I was trying to fecth was set to
So the workflow was as follows: 1) I did load entity
A; 2) entity
@OneToOnerelationship with entity
B, so the latter is loaded by “reflection”; 3) entity
@ManyToOnerelationship with entity
Cand tries to
EAGER-ly fetch it. The point 3 is, of course, the problem.
The solutionHaving identified the problem, hitting the solution was quite easy: change the
@ManyToOnefetching type from
LAZY. Please note that even setting
optional = truedid not work.
Again, I hate Hibernate with a passion!