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:alpha.portal.dao.hibernate.AlphaCardDaoHibernate.java

License:Apache License

/**
 * List alpha cards by criterion.// w  w  w  . ja v  a2 s .  c  o  m
 * 
 * @param caseId
 *            the case id
 * @param criteriaArray
 *            the criteria array
 * @return the list
 * @see alpha.portal.dao.AlphaCardDao#listAlphaCardsByCriterion(org.hibernate.criterion.Criterion)
 */
public List<AlphaCard> listAlphaCardsByCriterion(final String caseId, final Criterion... criteriaArray) {
    Session session;
    boolean sessionOwn = false;
    try {
        session = this.getSessionFactory().getCurrentSession();
    } catch (final Exception e) {
        session = this.getSessionFactory().openSession();
        sessionOwn = true;
    }

    // get newest sequenceNumber for each cardId
    final DetachedCriteria version = DetachedCriteria.forClass(AlphaCard.class, "cardVersion")
            .add(Property.forName("card.alphaCardIdentifier.cardId")
                    .eqProperty("cardVersion.alphaCardIdentifier.cardId"))
            .setProjection(
                    Projections.projectionList().add(Projections.max("alphaCardIdentifier.sequenceNumber")));

    final Criteria crit = session.createCriteria(AlphaCard.class, "card");
    for (final Criterion c : criteriaArray) {
        // Create the subquery (somehow we need to use the Descriptor or
        // else the subquery-JOIN is not done)
        final DetachedCriteria subcrit = DetachedCriteria.forClass(AlphaCardDescriptor.class, "crit");

        // Join the adornments
        subcrit.createAlias("crit.adornmentList", "ad");

        // Add adornment condition
        subcrit.add(c);

        // Map the subquery back to the outer query
        subcrit.add(Restrictions.eqProperty("card.alphaCardIdentifier", "crit.alphaCardIdentifier"));

        // Narrow down subquery or else we get a NullPointer-Exception
        subcrit.setProjection(Projections.property("crit.alphaCardIdentifier.cardId"));

        // Add this subquery to the outer query.
        crit.add(Subqueries.exists(subcrit));
    }
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
            .add(Property.forName("alphaCardIdentifier.sequenceNumber").eq(version))
            .createAlias("alphaCase", "case").add(Restrictions.eq("case.caseId", caseId));

    List<AlphaCard> list = crit.list();
    if (list.size() > 1) {
        final List<AlphaCard> order = (list.get(0)).getAlphaCase().getAlphaCards();
        final List<AlphaCard> orderedList = new LinkedList<AlphaCard>();
        for (final AlphaCard cc : order) {
            for (final AlphaCard c : list) {
                if (c.getAlphaCardIdentifier().equals(cc.getAlphaCardIdentifier())) {
                    orderedList.add(c);
                    break;
                }
            }
        }
        list = orderedList;
    }

    if (sessionOwn) {
        session.close();
    }

    return list;
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * /*from  w  w  w.  ja v  a  2  s  .c om*/
 * @param page
 * @param detachedCriteria
 * @param resultTransformer
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria, ResultTransformer resultTransformer) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    // set page
    if (!page.isDisabled()) {
        criteria.setFirstResult(page.getFirstResult());
        criteria.setMaxResults(page.getMaxResults());
    }
    // order by
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        for (String order : StringUtils.split(page.getOrderBy(), ",")) {
            String[] o = StringUtils.split(order, " ");
            if (o.length == 1) {
                criteria.addOrder(Order.asc(o[0]));
            } else if (o.length == 2) {
                if ("DESC".equals(o[1].toUpperCase())) {
                    criteria.addOrder(Order.desc(o[0]));
                } else {
                    criteria.addOrder(Order.asc(o[0]));
                }
            }
        }
    }
    page.setList(criteria.list());
    return page;
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * //w w  w.j  a  v  a 2s . com
 * @param detachedCriteria
 * @param resultTransformer
 * @return
 */
@SuppressWarnings("unchecked")
public List<T> find(DetachedCriteria detachedCriteria, ResultTransformer resultTransformer) {
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    return criteria.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public T findById(ID id, String... joinProps) {
    T myEntity = null;/*from www .  ja  va2 s . c  o  m*/
    Criteria criteria = null;

    try {
        criteria = getSession().createCriteria(this.persistenceClass);
        criteria.add(Restrictions.idEq(id));
        for (String prop : joinProps) {
            criteria.setFetchMode(prop, FetchMode.JOIN);
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        myEntity = (T) criteria.uniqueResult();
    } catch (Exception e) {
        throw new PersistenceException("Fehler beim lesen eines Entities: Class="
                + this.persistenceClass.getSimpleName() + " Key=" + id.toString() + "\n" + e.getMessage(), e);
    }

    return myEntity;

}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<T> findAllOrdered(Order... orders) {

    List<T> myList = null;/*from w w  w  .j a  v  a  2  s.  c  o m*/

    try {
        Criteria crit = getSession().createCriteria(this.persistenceClass);
        for (Order order : orders) {
            crit.addOrder(order);
        }
        crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        myList = crit.list();
    } catch (Exception e) {
        throw new PersistenceException("Failure during reading entities. Class="
                + this.persistenceClass.getSimpleName() + "\n" + e.getMessage(), e);
    }

    return myList;
}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<T> findByExample(T exampleInstance, String... excludeProps) {

    List<T> myList = null;/* ww w  . j av a  2s.  c  o m*/

    try {
        Criteria crit = getSession().createCriteria(this.persistenceClass);
        Example example = Example.create(exampleInstance);
        for (String exclude : excludeProps) {
            example.excludeProperty(exclude);
        }
        crit.add(example);
        // Tell Hibernate to remove duplicates from the result set if there
        // is a
        // OneToMany relation in the exampleInstance entity.
        crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        myList = crit.list();
    } catch (Exception e) {
        throw new PersistenceException("Failure during reading entities (by example). Class="
                + this.persistenceClass.getSimpleName() + "\n" + e.getMessage(), e);
    }

    return myList;
}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/**
 * Find several entities via criterion./* w  ww  . ja v  a2  s .com*/
 *
 * @param criterion beliebige Kriterien
 * @return (leere) Liste von Entities
 */
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
    Criteria crit = getSession().createCriteria(this.persistenceClass);
    for (Criterion c : criterion) {
        crit.add(c);
    }
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return crit.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.GenericHibernateDao.java

License:Apache License

/**
 * Search by use of the specified search criterions, order by specified
 * order criterions.//ww w.jav  a  2 s  . c o m
 *
 * @param criterions - search criterions
 * @param orders - order criterions
 * @param aliases list of aliasnames
 * @return List of result records
 */
@SuppressWarnings("unchecked")
protected List<T> findByCriteriaOrdered(Criterion criterions[], Order orders[], String aliases[]) {
    Criteria crit = getSession().createCriteria(this.persistenceClass);
    if (aliases != null) {
        for (String alias : aliases) {
            crit.createAlias(alias, alias);
        }
    }

    for (int i = 0; i < criterions.length; i++) {
        crit.add(criterions[i]);
    }
    for (int j = 0; j < orders.length; j++) {
        crit.addOrder(orders[j]);
    }
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return crit.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.project.TiTAProjectDao.java

License:Apache License

/** {@inheritDoc} **/
@SuppressWarnings("unchecked")
@Override//from   w  w w. j av  a2  s. c  o m
public List<TiTAProject> findProjectsOrderedByName(int maxResult, String orderBy) throws PersistenceException {
    Criteria crit = getSession().createCriteria(TiTAProject.class);

    if (maxResult > 0) {
        crit.setMaxResults(maxResult);
    }

    crit.add(Restrictions.eq("deleted", false));

    crit.addOrder(Order.asc(orderBy));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return crit.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.project.TiTAProjectDao.java

License:Apache License

/** {@inheritDoc} **/
@SuppressWarnings("unchecked")
@Override/*  ww w. j  a  va  2 s  . c om*/
public List<ProjectStatus> getAvailableProjectStati() throws PersistenceException {
    Criteria crit = getSession().createCriteria(ProjectStatus.class);

    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return crit.list();
}