Whithout using Spring we encountered the same problem.
To me it seems Hibernate works on criteria queries like this:
Your criteria query retrieves your user objects as was your intention, using the subcriteria on availability to restrict the set of Users objects.
To populate the availability hibernate still respects the mapping setting, which probably states to lazy initialize it.
Hibernate seems to ignore the FetchMode.JOIN in the criteria query when you use .createCriteria on that assoziation. Thus your LazyInitialization upon access.
At least thats what I understood from
http://www.hibernate.org/315.html.
My workaround was to use a filter. Your code would then look something like this:
Code:
private List getUsers(final Date start,final Date end){
return (List)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException{
session.enableFilter("myDateFilter").setParameter("startDate", start)
.setParameter("endDate",end);
Criteria c = session.createCriteria(Users.class)
.setFetchMode("availability", FetchMode.JOIN);
session.disableFilter("myDateFilter")
return c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}
});
}
Your mapping:
Code:
<filter-def name="myDateFilter">
<filter-param name="startDate" type="java.util.Date"/>
<filter-param name="endDate" type="java.util.Date"/>
</filter-def>
<set name="availability">
[... omitted your key, many-to-one and other tags, as i don't know them...]
<filter name="myDateFilter" condition=":startDate < dateTime and :endDate > dateTime"/>
</set>