Example usage for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY

List of usage examples for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY

Introduction

In this page you can find the example usage for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY.

Prototype

ResultTransformer DISTINCT_ROOT_ENTITY

To view the source code for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY.

Click Source Link

Document

Each row of results is a distinct instance of the root entity

Usage

From source file:alpha.portal.dao.hibernate.AlphaCardDaoHibernate.java

License:Apache License

/**
 * List alpha cards by criterion./* ww  w .  j av a 2 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:ar.com.zauber.commons.repository.CriteriaTranslator.java

License:Apache License

/** @see Translator#translate(Query) */
public final void translate(final Query<?> aQuery) {
    if (clazz == null) {
        clazz = ((SimpleQuery<?>) aQuery).getClazz();
    }/*from  w w  w.j  a  v a2s . c  om*/
    final SimpleQuery<?> simpleQuery = (SimpleQuery<?>) aQuery;

    final FilterVisitor filterVisitor = new CriteriaFilterVisitor(clazz, sessionFactory);
    ((SimpleQuery<?>) aQuery).getFilter().accept(filterVisitor);
    criteria = ((CriteriaFilterVisitor) filterVisitor).getCriteria();

    if (!ignoreOrder) {
        addOrder(simpleQuery.getOrdering());
    }

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (debugging) {
        logger.debug("Criteria: " + criteria);
    }

}

From source file:ar.com.zauber.commons.repository.query.visitor.CriteriaFilterVisitor.java

License:Apache License

/**
 * Crea el/la CriteriaVisitor.//from ww w  .  ja v a 2s .  c  o m
 *
 * @param aClazz clase a buscar.
 * @param aSessionFactory para obtener metadata.
 */
public CriteriaFilterVisitor(final Class<?> aClazz, final SessionFactory aSessionFactory) {
    criteria = DetachedCriteria.forClass(aClazz);
    criteriaForCount = DetachedCriteria.forClass(aClazz);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    clazz = aClazz;
    aliases = new HashMap<String, String>();
    sessionFactory = aSessionFactory;
}

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;//ww  w .j  a va  2s  . 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 ava  2s.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;//  www  .  ja v  a2 s  .  c om

    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./* ww  w. j av  a 2  s  .  c  o  m*/
 *
 * @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.  j av  a2 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/*ww w.  j ava 2 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//w  ww .  j a  v a 2s  .  c  o  m
public List<ProjectStatus> getAvailableProjectStati() throws PersistenceException {
    Criteria crit = getSession().createCriteria(ProjectStatus.class);

    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return crit.list();
}