Archive for the ‘Hibernate’ Category

Declarative transactions in Spring

August 28, 2007

Spring applicationContext.xml file for the sample projectSpring declarative transactions, and the DAO design pattern simplify database (& JMS) programming. With the DAO design pattern, you need to create your domain objects and encapsulate the database access in the DAO classes. This makes up the model part of your application. The model component also includes the business (aka manager layer) that captures the semantics of your application. The methods of the business layer need to be enlisted in the transaction. I’ve updated my sample project to include the business layer and a JUNIT test case for the transaction. (more…)

Cascaded delete with hibernate

August 26, 2007

When you’ve modeled a parent and child association with a hibernate mapping, you can configure the association so that when you delete a parent all of the children are also deleted. For an example, see these mappings and the test case (method – testCascadedDelete).

In a cascaded delete, first the children get deleted and finally the parent. But the children are deleted one at a time. Instead of say (delete from child where parentId=xx;). I wonder if there is a hibernate setting for optimizing MySQL statements (assuming that a single delete is more efficient than multiple ones).

Lazy initialization with Hibernate and Spring

August 26, 2007

If are new to Hibernate and Spring, you may have come across the following exception:


net.sf.hibernate.LazyInitializationException Failed to lazily initialize a
collection - no session or session was closed

This problem surfaces when you try to create a hibernate mapping to model a parent child association so that the database query to get the children is executed only when you attempt to access the set of children (and not before). This is the so called lazy initialization of a collection, the alternative is to perform eager initialization where the query to get the children is executed when the parent object is created.

To get lazy initialization to work, you need to understand how to configure Spring, how to create the correct hibernate mapping files, and in particular, the life cycle of the hibernate session object.

Spring provides a number of abstractions to make working with hibernate easier and more efficient. With respect to hibernate sessions, Spring offers a number of options depending on whether you’re writing a servlet or not. In the case of the servlet, you can use Spring’s OpenSessionInViewFilter class which ensures that the hibernate session is attached to the thread that is processing the entire HTTP request. If you’re writing an application or a JUNIT test case, for example, then you need to ensure that transaction synchronization is happening per thread. See the JUNIT setUp and tearDown methods here for how to do this.

I started writing a sample hibernate/spring application to test how lazy initialization and cascaded deletes work. The sources of this sample can be accessed here. Whist I was doing this work, I came across this excellent description on how to test hibernate and spring.

Spring & Hibernate’s not mapped problem

July 20, 2007

I’m sure that many hours must have been spent by Spring/Hibernate users trying to figure out why they are getting the table not mapped exception when they are sure that have mapped the table. Well the problem can be fixed if you take care of the case (upper vs. lower case). Suppose that the name of your table is user and the name of the your class is User.Here is the crux of the problem:This is the wrong version that leads to the not mapped exception:getHibernateTemplate().find("from user");Here is the correct version:getHibernateTemplate().find("from User");You see the problem is that in the hibernate version of the SQL, you are expected to use the name of your class and not the name of the table. Typically, the name of the table is all in lowercase and the name of the class starts with an uppercase. Yep programming can be very frustrating.