Today, I finally had time to spend a couple good, solid hours in the debugger, during which time I came to the conclusion that the problem my code was having was not in my own code. Sure, the exception was actually happening in an org.hibernate class, but what are the odds of encountering an honest to goodness Hibernate bug? Looks like I did find one, though: Hibernate Forums – View topic – NullPointerException – Set in Collection not initialized
From my forum posting, here are the steps that trigger this:
1) create a class with a lazy collection
2) Load up an entity of that class
3) do whatever you want, but don’t touch that lazy collection
4) flush the session
5) try to access that lazy collection
Hibernate is a great tool, and I’m very thankful that it’s open source. While I’m not averse to paying for good software, I wouldn’t want a library that does not include source. If I had to report this problem to a vendor and wait for them to a) agree that it’s a problem, b) fix it, c) release the fix, I’d be hurting. In this case, it was really easy to get the Hibernate source running in Eclipse, learn a bit about how collection initialization works, and find a seemingly reasonable place for a fix.
Update: This is not a Hibernate bug. It’s a mistake in my Interceptor code. Film at 11.