Posts

Showing posts from November, 2008

Hibernate's Criteria API

Hibernate has a Criteria API which allows you to dynamic build up filter conditions and set various join options. It is perfectly suited for flexible searches on entities, however there are areas in falls down in and the documentation is scarce.
Criteria instances can be either created from the hibernate Session or a DetachedCriteria can be created for later execution or for passing into Spring's HibernateTemplate.

The first thing that is explicitly mentioned is that references to properties can only be one deep, properties on child entities must be mapped via createAlias / createCriteria call. For example, the following will give a "org.hibernate.QueryException: could not resolve property: child.property of: com.example.model.Parent".
DetachedCriteria criteria = DetachedCriteria.forClass(Parent.class,"parent");
criteria.add(Restrictions.eq("parent.child.property", "somevalue");
Instead the child entity must be explicitly join by calling the foll…

Hibernate's Lazy Loading Quirks

I have come across two problems which have caused me no end of grief which turned out to be pretty simple.

LazyInitializationException
When using the Spring OpenSessionInViewFilter, I refer to lazily initialised properties (mapped as many-to-one or one-to-one) in the JSP pages. However I was getting a scenario where if a user performed a certain action I would get the following error:
org.hibernate.LazyInitializationException: could not initialize proxy - no SessionAfter a lot of debugging and sifting through hibernate's source I traced it to a bit of code I had used that someone had posted for having a paged interval collection for hibernate (as oracle doesn't support server side paging). The culprit line of code was:
hibernateTemplate.clear();

//which is defined in Spring's wrapper as
public void clear() throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) {
session.clear();
return null;
}
});
}
T…

EasyMock and Spring Autowiring

Spring Framework provides an easy way to unit test components in isolation and EasyMock provides a quick way to create mock objects with very little effort. Combining them together, Spring can be used to create the mock objects so that beans that use auto wiring to inject dependencies can be tested without having to modify the classes or create complex context configuration files.

Creating mock objects using spring is simple, here is a sample beans.xml:
<bean class="org.easymock.EasyMock" factory-method="createNiceMock" primary="true" id="someServiceMock"> <constructor-arg value="com.example.service.SomeService" /> </bean> This creates a mock object from the given interface. The primary is used to indicate that it should be used for autowiring which is useful when component scanning is used together with @Component tags. When you want to refer to the actual implementation you would use @Qualifier("someService&…