Skip to content

Using detached Criteria [SPR-1254] #5956

@spring-projects-issues

Description

@spring-projects-issues

Frank Verbruggen opened SPR-1254 and commented

My software developers use the HibernateTemplate and HibernateDAOSupport classes to combine Spring 1.2 final and Hibernate 3.
I wanted to hide all session management and transaction management from them, so we used a configured transaction interceptor, also by Spring.
All was well until we decided upon using the Criteria interface by Hibernate.
Since I hid the session management from my team they could not use Criteria objects, and there was no support in HibernateTemplate, nor in HibernateDAOSupport.
The next step was for me to take a looksy in the source code.
After some investigation, I found that support wasn't available, so I decided to use my own classes.
The following 2 methods were added to HibernateTemplate:

<code>
public CriteriaImpl getDetachedCriteria(Class persistentClass){
return new CriteriaImpl(persistentClass.getName(), null);
}

public List find(final CriteriaImpl detachedCriteria){
	return (List) execute(new HibernateCallback() {
		public Object doInHibernate(Session session) throws HibernateException {
			detachedCriteria.setSession((SessionImpl) session);
			return detachedCriteria.list();
		}
	}, true);		
}

</code>

and the following 2 protected methods were added to HibernateDaoSupport:

<code>
protected List list(Class domeinObject) {
CriteriaImpl crit = getHibernateTemplate().getDetachedCriteria(domeinObject);
addCriteria(crit);
return getHibernateTemplate().find(crit);
}

protected Criteria addCriteria(Criteria criteria) {
	return criteria;
}

</code>

now my developers can override addCriteria and call list with their domain object in implementing classes.

I have identified 2 major drawbacks to this approach:

  1. each implementing class can implement addCriteria only once, thus a State pattern is neccessary to perform different list calls.
  2. the explicit cast to SessionImpl that is needed because CriteriaImpl requires it in the setSession function

I hope this functionality will be added in the near future.
Yours sincerely,

Frank Verbruggen


Affects: 1.2 final

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions