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:dao.hibernate.HibernateWorkDAO.java

@Override
public List<Work> getUngradedWorks() throws EngineDAOException {
    getSession().beginTransaction();// ww w .  j  a  v a 2 s  . c  o m
    Criteria criteria = getSession().createCriteria(persistentClass);
    criteria.add(Restrictions.eq(GRADE, null));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<Work> works;
    try {
        works = criteria.list();
    } catch (RuntimeException e) {
        throw new EngineDAOException(e);
    }
    if (works == null) {
        getSession().getTransaction().rollback();
        throw new EngineDAOException(
                MessageFormat.format(ERROR_PERSON_NOT_FOUND_BY_USERNAME_AND_PASSWORD, null));
    }
    getSession().getTransaction().commit();
    return works;
}

From source file:dao.hibernate.HibernateWorkDAO.java

@Override
public Work getApprovedWorkByStudentWithoutFinalURI(Student student) throws EngineDAOException {
    getSession().beginTransaction();/*from w  w  w .  ja v a  2 s .com*/
    Criteria criteria = getSession().createCriteria(persistentClass);
    criteria.add(Restrictions.isNull(FINAL_FILE_URI));
    criteria.add(Restrictions.eq(STUDENT, student));
    criteria.add(Restrictions.eq(STATUS, APPROVED));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    Work work;
    try {
        work = (Work) criteria.uniqueResult();
        System.out.println(work);
    } catch (RuntimeException e) {
        throw new EngineDAOException(e);
    }
    if (work == null) {
        getSession().getTransaction().rollback();
        throw new EngineDAOException(
                MessageFormat.format(ERROR_PERSON_NOT_FOUND_BY_USERNAME_AND_PASSWORD, null));
    }
    getSession().getTransaction().commit();
    return work;
}

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

@SuppressWarnings("unchecked")
@Override/*from  www  . j  a  v  a 2 s  . com*/
public List<T> findAll() {
    Criteria criteria = getCriteria();
    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

@SuppressWarnings("unchecked")
@Override//  w  w  w  . j  a v a  2  s  . c  o  m
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  .jav  a2 s. co  m
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();/*  w  w  w.j a  v  a 2  s  .  com*/
    for (String association : associations) {
        criteria.setFetchMode(association, FetchMode.JOIN);
    }
    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();/*from w  ww  .j  ava  2s. c o  m*/
    for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
        criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));
    }
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<T> list = criteria.list();
    sort(list);
    return list;
}

From source file:de.congrace.blog4j.dao.AttachmentDaoImpl.java

License:Apache License

public List<FileAttachment> getFileAttachmentsByArticleId(long id) {
    return (List<FileAttachment>) this.getCurrentSession().createCriteria(FileAttachment.class)
            .createAlias("articles", "a").add(Restrictions.eq("a.id", id))
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();
    //       return (List<FileAttachment>) this.getCurrentSession().createCriteria(FileAttachment.class).createCriteria("articles").add(Restrictions.eq("id", id)).list();
    //       return (List<FileAttachment>) this.getCurrentSession().createQuery("from FileAttachment f join f.articles a where a.id=?").setLong(0, id).list();
}

From source file:de.congrace.blog4j.dao.AttachmentDaoImpl.java

License:Apache License

public List<URIAttachment> getURIAttachmentsByArticleId(long id) {
    return (List<URIAttachment>) this.getCurrentSession().createCriteria(URIAttachment.class)
            .createAlias("articles", "a").add(Restrictions.eq("a.id", id))
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();
    //       return (List<URIAttachment>) this.getCurrentSession().createCriteria(URIAttachment.class).createCriteria("articles").add(Restrictions.eq("id", id)).list();
    //       return (List<URIAttachment>) this.getCurrentSession().createQuery("from URIAttachment u join u.articles a where a.id=?").setLong(0, id).list();
}

From source file:debop4k.data.orm.hibernate.dao.HibernateDao.java

License:Apache License

public long count(@NonNull Query query, HibernateParameter... parameters) {
    Object result = CriteriaEx.setParameters(query, parameters)
            .setResultTransformer(CriteriaSpecification.PROJECTION)
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).uniqueResult();
    return Convertx.asLong(result);
}