Example usage for org.hibernate Query setResultTransformer

List of usage examples for org.hibernate Query setResultTransformer

Introduction

In this page you can find the example usage for org.hibernate Query setResultTransformer.

Prototype

@Deprecated
Query<R> setResultTransformer(ResultTransformer transformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<Person> getPersonsPaged(final int firstResult, final int maxResults) {
    @SuppressWarnings("unchecked")
    List<Person> persons = (List<Person>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from Person p where p.dateVoided is null order by p.personId");
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            log.debug("Querying using " + query.toString());
            List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            removeDeletedIdentifiers(list);
            log.debug("Query by criteria returned: " + list.size() + " elements.");
            return list;
        }/*from w  w w  .java 2s. c  om*/
    });
    return persons;
}

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<Record> getRecordsPaged(final int firstResult, final int maxResults) {
    @SuppressWarnings("unchecked")
    List<Person> persons = (List<Person>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("from Person p where p.dateVoided is null order by p.personId");
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            log.debug("Querying using " + query.toString());
            List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            removeDeletedIdentifiers(list);
            log.debug("Query by criteria returned: " + list.size() + " elements.");
            return list;
        }/*from w  ww .ja v a 2s . c  om*/
    });
    return convertPersonToRecord(persons);
}

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<IdentifierUpdateEvent> getIdentifierUpdateEvents(final int startIndex, final int maxEvents,
        final User eventRecipient) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unchecked")
    List<IdentifierUpdateEvent> identifierUpdateEvents = (List<IdentifierUpdateEvent>) getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery(
                            "from IdentifierUpdateEvent i where i.updateRecipient = :updateRecipient order by i.identifierUpdateEventId");
                    query.setParameter("updateRecipient", eventRecipient);
                    query.setFirstResult(startIndex);
                    query.setMaxResults(maxEvents);
                    log.debug("Querying using " + query.toString());
                    List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                            .list();//from   w  w  w . j a v  a  2  s. com
                    log.debug("Query returned: " + list.size() + " elements.");
                    return list;
                }
            });
    return identifierUpdateEvents;
}

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<IdentifierUpdateEvent> getIdentifierUpdateEvents(final User eventRecipient) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unchecked")
    List<IdentifierUpdateEvent> identifierUpdateEvents = (List<IdentifierUpdateEvent>) getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery(
                            "from IdentifierUpdateEvent i where i.updateRecipient = :updateRecipient order by i.identifierUpdateEventId");
                    query.setParameter("updateRecipient", eventRecipient);
                    log.debug("Querying using " + query.toString());
                    List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                            .list();//  w w  w . j a v  a2s.  c o  m
                    log.debug("Query returned: " + list.size() + " elements.");
                    return list;
                }
            });
    return identifierUpdateEvents;
}

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<IdentifierUpdateEvent> getIdentifierUpdateEventsByDate(final Date startDate,
        final User eventRecipient) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unchecked")
    List<IdentifierUpdateEvent> identifierUpdateEvents = (List<IdentifierUpdateEvent>) getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery(
                            "from IdentifierUpdateEvent i where i.dateCreated > :startDate and i.updateRecipient = :updateRecipient "
                                    + "order by i.dateCreated");
                    query.setParameter("startDate", startDate);
                    query.setParameter("updateRecipient", eventRecipient);
                    log.debug("Querying using " + query.toString());
                    List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                            .list();//from  ww  w  .  j av a2s . c o  m

                    log.debug("Query returned: " + list.size() + " elements.");
                    return list;
                }
            });
    return identifierUpdateEvents;
}

From source file:org.openhie.openempi.dao.hibernate.PersonDaoHibernate.java

License:Open Source License

public List<IdentifierUpdateEvent> getIdentifierUpdateEventsBeforeDate(final Date startDate,
        final User eventRecipient) {
    // TODO Auto-generated method stub
    @SuppressWarnings("unchecked")
    List<IdentifierUpdateEvent> identifierUpdateEvents = (List<IdentifierUpdateEvent>) getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery(
                            "from IdentifierUpdateEvent i where i.dateCreated < :startDate and i.updateRecipient = :updateRecipient "
                                    + "order by i.dateCreated");
                    query.setParameter("startDate", startDate);
                    query.setParameter("updateRecipient", eventRecipient);
                    log.debug("Querying using " + query.toString());
                    List<Person> list = (List<Person>) query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                            .list();//from  ww  w.j ava 2s.  c  om

                    log.debug("Query returned: " + list.size() + " elements.");
                    return list;
                }
            });
    return identifierUpdateEvents;
}

From source file:org.opentaps.foundation.entity.hibernate.Session.java

License:Open Source License

/**
 * Creates a Hibernate Query from the queryString. Check if the queryString
 * is for an entity which is an entity engine view-entity and if it is,
 * transfrom the HQL to native SQL first, then return it as the Query. If
 * not, just return the Query from the queryString.
 *
 * @param queryString a <code>String</code> value.
 * @return a <code>Query</code> value
 * @throws HibernateException if an error occurs
 *//*from  ww w  .j  av a2 s  .  c om*/
public Query createQuery(String queryString) throws HibernateException {
    try {
        // Now check if the entity is a view-entity
        Entity entity = HibernateUtil.getEntityInstanceByQueryString(queryString);
        // encryptFields of this GenericValue
        List<String> encryptParameters = HibernateUtil.getEncryptParametersByQueryString(queryString,
                delegator);
        if (entity.isView()) {
            // if it is a view-entity, we should transform hql to native sql
            // query.
            String nameQueryString = getNamedQuery("select" + entity.getClass().getSimpleName() + "s")
                    .getQueryString();
            String sqlString = nameQueryString + " "
                    + HibernateUtil.hqlToSql(queryString, HibernateUtil.retrieveClassName(queryString),
                            HibernateUtil.retrieveClassAlias(queryString),
                            entity.fieldMapColumns.get(entity.getClass().getSimpleName()));
            Debug.logVerbose("Querying [" + entity.getBaseEntityName() + "] with query [" + sqlString + "]",
                    MODULE);
            org.hibernate.Query hibernateQuery = hibernateSession.createSQLQuery(sqlString);
            // set result transformer to change result to the class of entity
            hibernateQuery.setResultTransformer(OpentapsTransformer.aliasToBean(entity.getClass()));
            Query query = new Query(hibernateQuery, entity.getBaseEntityName(), encryptParameters, crypto);
            return query;
        } else {
            // normal hql, should create a hibernate query and return it.
            org.hibernate.Query hibernateQuery = hibernateSession.createQuery(queryString);
            Query query = new Query(hibernateQuery, entity.getBaseEntityName(), encryptParameters, crypto);
            return query;
        }
    } catch (InstantiationException e) {
        Debug.logError(e, MODULE);
        throw new HibernateException(e.getMessage());
    } catch (IllegalAccessException e) {
        Debug.logError(e, MODULE);
        throw new HibernateException(e.getMessage());
    } catch (ClassNotFoundException e) {
        Debug.logError(e, MODULE);
        throw new HibernateException(e.getMessage());
    }
}

From source file:org.ow2.bonita.persistence.db.DbSessionImpl.java

License:Open Source License

@Override
public Set<EventCouple> getEventsCouples() {
    final Query query = getSession().getNamedQuery("getEventsCouples");

    query.setLong("current", System.currentTimeMillis());
    query.setResultTransformer(Transformers.aliasToBean(EventCouple.class));

    final Set<EventCouple> result = new HashSet<EventCouple>();
    CollectionUtils.addAll(result, query.iterate());
    return result;
}

From source file:org.sakaiproject.profile2.dao.impl.ProfileDaoImpl.java

License:Educational Community License

/**
  * {@inheritDoc}//from   www.j av a 2 s  .c  o  m
  */
public List<UserProfile> getUserProfiles(final int start, final int count) {

    //get fields directly from the sakaiperson table and use Transformers.aliasToBean to transform into UserProfile pojo
    //the idea is we *dont* want a SakaiPerson object
    HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {

            Query q = session.getNamedQuery(QUERY_GET_SAKAI_PERSON);

            //see scalars in the hbm
            q.setFirstResult(start);
            q.setMaxResults(count);
            q.setResultTransformer(Transformers.aliasToBean(UserProfile.class));
            q.setCacheMode(CacheMode.GET);
            return q.list();
        }
    };

    return (List<UserProfile>) getHibernateTemplate().executeFind(hcb);
}

From source file:org.squashtest.tm.service.internal.batchexport.ExportDao.java

License:Open Source License

@SuppressWarnings("unchecked")
private <R> List<R> findModels(Session session, String query, List<Long> tcIds, Class<R> resclass) {
    Query q = session.getNamedQuery(query);
    q.setParameterList("testCaseIds", tcIds, LongType.INSTANCE);
    q.setResultTransformer(new EasyConstructorResultTransformer(resclass));
    return q.list();
}