There's a fairy tale written by Hans Christian Anderson entitled The Emperor's New Clothes. In a nutshell, the story is about two smooth talking tailors who convince the Emperor that the invisible clothes they have made for him are the most exquisite in all of the kingdom. The tailors added that anyone who couldn't see how remarkable the clothes were was obviously a fool. Not wanting to be thought a fool the Emperor "dons" the clothing, and strolls through town completely naked. All of the townsfolk, not wanting to be thought fools either, praise the craftsmanship of the clothing. Only a single boy in the kingdom has the courage to say that the Emperor is not wearing any clothes.
Well, I just have this to say, "Hibernate, put some clothes on!"
I had another run-in with Hibernate the other day. I was working with a very simply database model.
I was using Hibernate to do the normal CRUD operations. Until I tried to do something fancy.
I wanted to insert a new Item and a new ItemType at the same time. I know what you're thinking, "That's Crazy!". Hibernate thought I was crazy too. It complained of a foreign key constraint violation. I spent two days trying to solve this problem, thinking the whole time how much easier it would be to use JDBC.
I solicited SweetTea’s help. He suggested that I set the cascade attribute on the many-to-one relationship to "none". That didn’t fix the problem. He also suggested that I re-fetch the new ItemType after I inserted it. That didn’t fix the problem either. Sorry, SweetTea. Your two years of Hibernate experience aren’t enough for this problem.
So, I checked online, and I found lots of tutorials/reviews written by enamored Hibernate fan-boys. Saying things like:
"Hibernate is simply the greatest object-relational mapping tool available for Java. I wrote an order application in just a few lines of code. I can’t imagine how hard it would have been to use straight JDBC."
Really? I can. It’s not that hard. I’m sure I can do a crazy two table insert in less than two days.
Thankfully, DeviousBard saved the day. Whoosh! After he was finished laughing at me for having to work with Hibernate, he dug in and started tweaking the Hibernate configuration files. Between us, DeviousBard and I have about 20 years of Java experience, but we must be complete idiots. We were pretty much changing settings randomly, and getting unexpected results.
- Lazy initialization Errors
- Batch Update Exceptions
- Class Cast Exceptions
- OMG Hibernate Sucks Exceptions
A few hours later, DeviousBard noticed something that appeared to be completely unrelated. A few lines above my troubled code, I was fetching an object by ID from one of the tables that I was attempting to insert into. The object was not found, and Hibernate simply returned null. Not a problem. Right? Wrong! This was the problem. Our best guess is that because the fetch was done within the same transactional context as the insert, Hibernate attempted to perform some sort of CRUD activity on the null object. By simply removing the seemingly unrelated code, my code suddenly started working. Shoot me.
I’m usually interested in knowing why and how things work. Usually when I start using a new API, I dig into the source, and have a look around. I have no interest in Hibernate internals. It’s like trying to figure out why a turd stinks.
I think I’m gonna write an Eclipse plugin called "WakeUp". Its sole purpose is to convert a Hibernate configuration file and the associated Java code back into POJO's that use JDBC. Then maybe, when everyone else in the Java world is willing to admit that Hibernate has no clothes, I can become a millionaire by selling the product that brings them back to reality.
You're welcome.
ReplyDeleteI don't even know why I read this blog.
ReplyDeleteEnough already with the Geek shit.
ReplyDeleteOK! With one post I've lost two of my six regular readers. Tough crowd.
ReplyDeleteHibernate may not be simple minds, like yourself :)
ReplyDeleteLook I found your blog again!
Blah, "for simple" ...
ReplyDeleteI'm inviting laughter, no doubt.
Yes, the irony is beautiful. :)
ReplyDelete