001: package org.osbl.persistence.hibernate;
002:
003: import org.osbl.persistence.*;
004: import org.hibernate.*;
005: import org.hibernate.transform.ResultTransformer;
006: import org.hibernate.criterion.*;
007:
008: import java.util.*;
009:
010: public class HibernateSimpleQueryCommand<T> extends
011: SimpleQueryCommand<T> implements HibernateCommand {
012: SessionFactory sessionFactory;
013:
014: public void setSessionFactory(SessionFactory sessionFactory) {
015: this .sessionFactory = sessionFactory;
016: }
017:
018: public List<T> execute() {
019: Session session = sessionFactory.getCurrentSession();
020:
021: Criteria criteria = session.createCriteria(type);
022:
023: for (Filter filter : filters) {
024: switch (filter.getOperator()) {
025: case EQUAL:
026: criteria.add(Restrictions.eq(filter.getProperty(),
027: filter.getValue()));
028: break;
029: case NOT_EQUAL:
030: criteria.add(Restrictions.ne(filter.getProperty(),
031: filter.getValue()));
032: break;
033: case GREATER_OR_EQUAL:
034: criteria.add(Restrictions.ge(filter.getProperty(),
035: filter.getValue()));
036: break;
037: case GREATER_THAN:
038: criteria.add(Restrictions.gt(filter.getProperty(),
039: filter.getValue()));
040: break;
041: case LESS_OR_EQUAL:
042: criteria.add(Restrictions.le(filter.getProperty(),
043: filter.getValue()));
044: break;
045: case LESS_THAN:
046: criteria.add(Restrictions.lt(filter.getProperty(),
047: filter.getValue()));
048: break;
049: case LIKE:
050: criteria.add(Restrictions.like(filter.getProperty(),
051: filter.getValue()));
052: break;
053: case LIKE_IGNORE_CASE:
054: criteria.add(Restrictions.ilike(filter.getProperty(),
055: filter.getValue()));
056: break;
057: case NULL:
058: criteria.add(Restrictions.isNull(filter.getProperty()));
059: break;
060: case NOT_NULL:
061: criteria.add(Restrictions.isNotNull(filter
062: .getProperty()));
063: break;
064: case IN:
065: criteria.add(Restrictions.in(filter.getProperty(),
066: (Collection) filter.getValue()));
067: break;
068: case NOT_IN:
069: criteria.add(Restrictions
070: .not(Restrictions.in(filter.getProperty(),
071: (Collection) filter.getValue())));
072: break;
073: }
074: }
075:
076: for (Map.Entry<String, Sort> entry : sorts.entrySet()) {
077: switch (entry.getValue()) {
078: case ASCENDING:
079: criteria.addOrder(Order.asc(entry.getKey()));
080: break;
081: case DESCENDING:
082: criteria.addOrder(Order.desc(entry.getKey()));
083: break;
084: }
085: }
086:
087: for (Map.Entry<String, Fetch> entry : fetches.entrySet()) {
088: switch (entry.getValue()) {
089: case JOIN:
090: criteria.setFetchMode(entry.getKey(), FetchMode.JOIN);
091: break;
092: case SELECT:
093: criteria.setFetchMode(entry.getKey(), FetchMode.SELECT);
094: break;
095: }
096: }
097: if (fetches.size() > 0)
098: criteria
099: .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
100:
101: return (List<T>) criteria.list();
102: }
103: }
|