2015-08-09

The number one mistake when using Hibernate

Hibernate is one of the most popular ORM solutions for Java, yet many developers are surprised when they find out that it is state based not SQL based framework.

This means that when you call Hibernate methods like session.save(entity), session.update(entity) etc. it does not go directly to the DB to issue an INSERT/UPDATE etc. but only associates the object passed as an argument with its session and assigns some internal state to it.

At a later point in time (typically before transaction commits) Hibernate performs a flush - it synchronizes the state of objects held in memory with the database, executing any number of INSERT/UPDATE/DELETE statements.

Hibernate is very easy to use. IMHO it is even too easy to use, because it makes an impression, that it is a simple framework. It's definitely not, there is a lot of complexity under the hood and if you ignore it, you can easily shoot yourself in the foot.