Hibernate Second Level Cache

When I have to develop a more advanced web application I tend to prefer java over any other language. Ok I have to admit, I haven’t tried ruby on rails yet, but I’ll wait for Thor to finish his nice plugin for netbeans 🙂
I just find that java has almost a nice architectural solution for every problem you face (server side that is). That’s also the downside, there are so many solutions out there, that for a new programmer its a bit overwhelming!

That said, I use Hibernate in almost all my projects. (hibernate being an orm and speeding up my development) As you might know Hibernate has caching built in. First level is the cache in your session, and it’s local for you. Second level cache is for performing queries that in most cases return the same result. Now in my case I created the query and passed it a long some parameters. The parameters were offcourse also hibernate mapped objects. I setup the objects to be cacheable and I set the query to be cached. But it still went to the database on every request!?! Strange it should work?!?
So I started digging and after a while found the problem, really stupid (offcourse). I generated my object model using the reverse enginering tools from hibernate. I thought I had overridden all the equals and hascodes of these objects (as you should always do), but it seemed for these objects I passed in as parameters I hadn’t done it yet.

So as I understand, hibernate puts you query in a container, along with all the parameters you pass to the query, and a reference to the resultset. So if you try to perform the query again, he checks if this is already in the map, but because my parameters always returned false when actually business wise being true. He kept on going back to the db. So you see, even the smallest most stupidest things can have a great impact on your whole thing. This case being overriding the equals and hashcode in a small (3 properties) object, made my app lose 0.3 seconds in a lot of requests.

But BUGFIX/ENHANCEMENT has been done :))