Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@SuppressWarnings("unchecked")
@Override//w w  w.ja  v a  2  s  .c  om
public Page<T> findAllFetchEagerly(Pageable pageable, Set<Criterion> criterions, String... associations) {
    //http://stackoverflow.com/questions/2183617/criteria-api-returns-a-too-small-resultset

    //get the ids of every object that matches the pageable conditions
    //we cannot get the objects directly because we use FetchMode.JOIN which returns the scalar product of all rows in all affected tables
    //and CriteriaSpecification.DISTINCT_ROOT_ENTITY does not work on SQL Level but on in Java after the result is returned from SQL
    Criteria criteria = getPageableCriteria(pageable);
    if (criterions != null) {
        for (Criterion c : criterions) {
            criteria.add(c);
        }
    }
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<Long> list = criteria.list();

    //once we have the required ids we query for the complete objects
    Criteria objectCriteria = getCriteria();
    for (String association : associations) {
        objectCriteria.setFetchMode(association, FetchMode.JOIN);
    }
    if (!list.isEmpty()) {
        objectCriteria.add(Restrictions.in("id", list));
    }
    objectCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    addOrderBy(objectCriteria, pageable);
    List<T> objects = objectCriteria.list();
    sort(objects);

    //we also need the total number of rows
    Criteria rowCountCritieria = getCriteria();
    if (criterions != null) {
        for (Criterion c : criterions) {
            rowCountCritieria.add(c);
        }
    }
    rowCountCritieria.setProjection(Projections.rowCount());
    Long resultCount = (Long) rowCountCritieria.uniqueResult();
    if (resultCount == null) {
        resultCount = objects.size() + 0L;
    }
    Collections.sort(objects);
    PageImpl<T> page = new PageImpl<>(new ArrayList<>(objects), pageable, resultCount);
    return page;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@SuppressWarnings("unchecked")
@Override/*from  w w  w.  j  a va 2s  .  c  om*/
public List<T> findAll(List<Long> ids) {
    Criterion[] criterion = new Criterion[ids.size()];
    for (int i = 0; i < ids.size(); i++) {
        criterion[i] = Restrictions.eq("id", ids.get(i));
    }
    Disjunction or = Restrictions.or(criterion);
    Criteria criteria = getCriteria();
    criteria.add(or);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<T> list = (List<T>) criteria.list();
    sort(list);
    return list;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@Override
public Page<T> findAllByFuzzySearch(String search, Set<Criterion> criterions, String... associations) {
    Criteria criteria = getCriteria();
    for (String association : associations) {
        criteria.setFetchMode(association, FetchMode.JOIN);
    }//from   ww  w  .j  a  v  a  2 s .  co  m
    List<Criterion> predicates = new ArrayList<>();
    for (String indexedPropery : getIndexedProperties()) {
        if (!StringUtils.isEmpty(search)) {
            String[] searchTerms = search.split(" ");
            for (String searchTerm : searchTerms) {
                predicates.add(Restrictions.ilike(indexedPropery, searchTerm, MatchMode.ANYWHERE));
            }
        }
    }
    if (!predicates.isEmpty()) {
        criteria.add(Restrictions.or(predicates.toArray(new Criterion[predicates.size()])));
    }
    if (criterions != null) {
        for (Criterion c : criterions) {
            criteria.add(c);
        }
    }
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<T> objects = criteria.list();
    sort(objects);
    PageImpl<T> page = new PageImpl<>(objects);
    return page;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@Override
public List<T> findByAttributes(Map<String, Object> attributeMap) {
    Criteria criteria = getCriteria();
    for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
        criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));
    }//from  ww  w .  ja  va  2  s  .  c o  m
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<T> list = criteria.list();
    sort(list);
    return list;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@Override
public List<T> findAllFetchEagerly(String... associations) {
    Criteria crit = getCriteria();
    for (String association : associations) {
        crit.setFetchMode(association, FetchMode.JOIN);
    }//from  w  w  w.j  a v a2 s . c  o m
    //we only want unique results
    //see http://stackoverflow.com/questions/18753245/one-to-many-relationship-gets-duplicate-objects-whithout-using-distinct-why
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<T> list = (List<T>) crit.list();
    sort(list);
    return list;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@Override
public List<T> findAllFetchEagerlyWithAttributes(Map<String, Object> attributeMap, String... associations) {
    Criteria crit = getCriteria();
    for (String association : associations) {
        crit.setFetchMode(association, FetchMode.JOIN);
    }//from w w w  .  j a v  a2s .co m
    for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
        crit.add(Restrictions.eq(entry.getKey(), entry.getValue()));
    }
    //we only want unique results
    //see http://stackoverflow.com/questions/18753245/one-to-many-relationship-gets-duplicate-objects-whithout-using-distinct-why
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<T> list = (List<T>) crit.list();
    sort(list);
    return list;
}

From source file:de.decidr.model.commands.workflowmodel.WorkflowModelCommand.java

License:Apache License

/**
 * Fetches the current deployed version of the workflow model.
 * //  www  . j  a v a  2  s . com
 * @param session
 *            current Hibernate Session
 * @return the current deployed version of the workflow model or null if
 *         there is no current deplyoed version.
 */
public DeployedWorkflowModel fetchCurrentDeployedWorkflowModel(Session session) {
    Criteria crit = session.createCriteria(DeployedWorkflowModel.class, "dwm");

    crit.createCriteria("originalWorkflowModel", "owm")
            .add(Restrictions.eqProperty("owm.version", "dwm.version"));

    crit.add(Restrictions.eq("originalWorkflowModel.id", getWorkflowModelId()));

    return (DeployedWorkflowModel) crit.setResultTransformer(CriteriaSpecification.ROOT_ENTITY).uniqueResult();
}

From source file:de.fau.osr.core.db.dao.impl.AbstractDefaultDao.java

License:Open Source License

/**
 * @return all objects of this Entity type
 *//*from w  w w  .  j ava2  s  .c  o  m*/
@SuppressWarnings("unchecked")
protected List<EntityClass> getAllObjects() {

    List<EntityClass> objects = new ArrayList<>();
    Session session = null;
    try {
        session = sessionFactory.openSession();

        Criteria criteria = session.createCriteria(getEntityType());
        //make uniq, criteria returns many double entities, because of fetched relations
        //http://stackoverflow.com/questions/8758363/why-session-createcriteriaclasstype-list-return-more-object-than-in-list
        criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

        objects = criteria.list();

    } catch (RuntimeException re) {
        re.printStackTrace();

    } finally {
        if (session != null) {
            session.close();
        }
    }

    return objects;
}

From source file:de.iew.framework.persistence.hibernate.HbmMessageBundleDaoImpl.java

License:Apache License

public List<Locale> getSupportedLocales() {
    Criteria crit = getCurrentSession().createCriteria(TextItem.class).setCacheable(true)
            .setProjection(Projections.distinct(Projections.projectionList()
                    .add(Projections.property("languageCode")).add(Projections.property("countryCode"))));
    crit.setCacheable(true);//from ww  w  .j  a  v  a2  s.co  m
    crit.setResultTransformer(LocaleTupleResultTransformer.DEFAULT);
    return crit.list();
}

From source file:de.iteratec.iteraplan.persistence.dao.GenericBaseDAO.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<E> loadElementList(final String orderByProperty) {

    HibernateCallback<List<E>> callback = new HibernateCallback<List<E>>() {
        public List<E> doInHibernate(Session session) {
            Criteria c = session.createCriteria(getPersistentClass());
            if (orderByProperty != null) {
                c.addOrder(Order.asc(orderByProperty));
            }//  w ww.  ja  va2s  . com
            c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            return c.list();
        }
    };

    return getHibernateTemplate().executeFind(callback);
}