Hibernate is a mature ORM tool for Java environments and is Free under LGPL. Hibernate lets you map your Java classes to database tables, and also provides data query and retrieval facilities. This article will focus on data query and retrieval facilities. I will not go into detail about how a query might be built and instead focus on some other things that a developer should have in mind.
The term session is a central part of the framework. The sessions lifecycle can span several database transactions and is the place where you create, read, update, delete instances of your mapped entity classes. Instances of your java classes can exist in three different states. Transient: An instance is transient when it at no point in time has been persisted with the session e.g when a new instance is made in your application. Persitent: An instance is persistent when it is associated with a session. Detached: The instance has previously been persisted but is currently not associated with a session.
The Session interface provides several different methods for moving instances beetween the different states. For example if you have a transient instance you can make it persistent by calling save(). To make an instance detached you can call delete(), however some would say that delete will make the instance transient but I don’t agree on this. If you want to make a detached instance persistent again you can call for example update(). The persistent instances are written to the database when the session is flushed. To clarify, when you retrieve an instance from the database using for example get() the instance is currently persisted and associated with the session. You then make some changes to the instance, in the same session, and these changes will be written to the database when the session is flushed, there is no need in this case to use a method like update on the instance. A flush will at the latest be made when the transaction is commited as long as you don’t change the default flush mode. Hibernate will generate the proper sql for the methods mentioned above.
Hibernate uses write behind wich means that sql operations scattered through the session are gathered together and executed by Hibernate when a flush is made. Hibernate has a set of rules for the order in wich the statements will be executed, see Hibernate doc for more information. One benefit using write behind is that nework traffic is reduced.
There are 3 different ways for querying the database except using the available methods in the Session interface. HQL(Hibernate Query Language), Using the Crtieria API or you can use native SQL, personally I prefer HQL. HQL syntax is similar to SQL and is object oriented wich means it uses objects and properties and their associations. You use the Query interface when working with HQL and it’s list() method is usually used to execute the query. The Query.list() method will invoke a flush so that no stale data will be returned. You have to be careful however when you mix native sql queries along with the previously mentioned methods in the same session since the sql might query or insert data into a table that has not yet been synchronized with Hibernate, remember that Hibernate is using write behind. For example you might use the save/update method in a session and then in that same session query a table using native sql where the instance later will be inserted/updated, this can lead to unexpected results e.g stale data. Therefore I recommend making an explicit flush before executing a SQL query or for that matter execute any query that is not using Query.list(). Making an explicit flush does not however mean that the data is commited.
