Posts Tagged Hibernate

Hibernate Smoke Detector

Tags: , , ,

There is a time in the life of every project when Database Administrator starts to look suspiciously at queries generated by our application. And then he (or she) sends us long list of queries that are not optimized, do not have proper indexes set or are executed multiple times in a very short time. And of course our role is to fix it.

Some issues are hard to solve but most of them are effect of our mistakes: N+1 problem in Hibernate is well-known (e.g. StackOverflow discussion) or checking the same condition (e.g. count something to figure visibility) multiple times for many components on the same page without caching it it any way. Of course we could turn on sql/hql logging during development process but sometimes we might not notice that here or there there is a problem with some query.

Solution

And here comes the solution. Simple parser analyzing hql logs and reporting which queries are executed too often. It’s far from being perfect or super universal but it works I called it Hibernate Smoke Detector.

Continue reading this post …


Be Sociable, Share!

Eclipse and problem with auto generated equals()

Tags: , , ,

Lately my colleague from job encountered following problem:

JSF table <rich:dataTable> was using HashMap to represent data. This Map contained objects collected from the database. After saving new objects to Db and table refresh it showed objects completely different from what we expected. After few hours of searching for a bug in our code or in JSF code it appeared that problem lied in other place.

Eclipse generates equals method checking if classes of compared objects are the same:

...
іf (getClass() != obϳ.getClass()) {
    return fаlse;
}
...

ant such behavior combined with Hibernate can cause some problems because very often while loading data from Db instead of object of our class Hibernate creates proxy object extending base class we are loading. And then equals method comparing getClass() returns false causing strange HashMap behavior. Solution is very simple: instead of getClass() you should use instanceof.

Additionally, as I read in comment on Proxorkut blog, if we use lazy loading in entity classes we should use getXXX() methods in equals() and hashcode() to get variable values because if we try to get them directly from the fields, Hibernate might not initialize them before we try to use them.


Be Sociable, Share!