01: package org.osbl.persistence.hibernate;
02:
03: import org.osbl.persistence.QueryCommand;
04: import org.hibernate.*;
05:
06: import java.util.List;
07:
08: public class HibernateQueryCommand<T> extends QueryCommand<T> implements
09: HibernateCommand {
10: SessionFactory sessionFactory;
11: String query;
12:
13: public void setSessionFactory(SessionFactory sessionFactory) {
14: this .sessionFactory = sessionFactory;
15: }
16:
17: public HibernateQueryCommand setQuery(String query) {
18: this .query = query;
19:
20: Session session = sessionFactory.getCurrentSession();
21: type = session.createQuery(this .query).getReturnTypes()[0]
22: .getReturnedClass();
23:
24: return this ;
25: }
26:
27: public Class<T> getType() {
28: return type;
29: }
30:
31: public List<T> execute() {
32: Session session = sessionFactory.getCurrentSession();
33:
34: if (query != null)
35: return session.createQuery(this.query).list();
36: else
37: return session.createCriteria(type).list();
38: }
39: }
|