Monday, May 30, 2011

Fun with Hibernate: Retrieving an object that should have no references

So I think I'm just going to call this a series, since there seem to be a lot of episodes of me and Hibernate not getting along.

This particular problem arose a couple weeks ago. It turned out that when you were on the website and clicked on a recording to bring up its detail page, a NullPointerException would be thrown. Great! There wasn't an obvious solution so I just logged it in our issue tracker (issue #68)

Then it looked like it only caused a problem with a specific recording (with id=4). Even more exciting was that the spot where the exception occurred was completely unrelated to the cause. Instead, it was being generated in the data layer when the query was made. Well that's never good.

Eventually we found that our Recording object, which contained a List of SongInstances, was causing the problem. One of the SongInstance objects was null. The weird part was that each SongInstance had a foreign key to a Recording. Since no-one had a reference to SongInstance, deleting a SongInstance should not have impacted anything.

Well, turns out the problem was caused by two things. First, one of our 'integration' tests tested deletion from the database (oops) and we don't have a test database yet. So it deleted one of the entries from Recording #4. The other problem was that our hibernate mapping Recording contained the following entry for mapping a list of SongInstances:

<list-index column="trackListing" base="1" />

Which caused Hibernate to retrieve the SongInstances sequentially by their "track listing." And when the test ran, it deleted the song instance that was set as track #2, causing a null pointer when Hibernate retrieved it. Oops.

So, our problem wasn't really a bug, but a side-effect of our test needing a real test database. Just one more thing to learn I guess.

In other news: we are setting a 'deadline' for feature-completeness by the end of June. Our schedule is progressing pretty well and we should be ready for The Next Step at that time. Yay!

See you next time.

No comments:

Post a Comment