Example usage for org.hibernate.criterion Projections distinct

List of usage examples for org.hibernate.criterion Projections distinct

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections distinct.

Prototype

public static Projection distinct(Projection projection) 

Source Link

Document

Create a distinct projection from a projection.

Usage

From source file:com.ut.tekir.contact.ContactBrowseBean.java

License:LGPL

@Override
public DetachedCriteria buildCriteria() {
    DetachedCriteria crit = DetachedCriteria.forClass(Contact.class);

    crit.setProjection(Projections.distinct(Projections.projectionList().add(Projections.property("id"), "id")
            .add(Projections.property("code"), "code").add(Projections.property("name"), "name")
            .add(Projections.property("fullname"), "fullname").add(Projections.property("ssn"), "ssn")
            .add(Projections.property("company"), "company").add(Projections.property("taxNumber"), "taxNumber")
            .add(Projections.property("taxOffice"), "taxOffice").add(Projections.property("title"), "title")
            .add(Projections.property("representative"), "representative")
            .add(Projections.property("info"), "info").add(Projections.property("exCode1"), "exCode1")
            .add(Projections.property("exCode2"), "exCode2").add(Projections.property("allType"), "allType")
            .add(Projections.property("customerType"), "customerType")
            .add(Projections.property("providerType"), "providerType")
            .add(Projections.property("agentType"), "agentType")
            .add(Projections.property("personnelType"), "personnelType")
            .add(Projections.property("branchType"), "branchType")
            .add(Projections.property("contactType"), "contactType")
            .add(Projections.property("bankType"), "bankType")
            .add(Projections.property("relatedType"), "relatedType")
            .add(Projections.property("foundationType"), "foundationType")));

    crit.setResultTransformer(Transformers.aliasToBean(ContactModel.class));

    if (filterModel.getCode() != null && filterModel.getCode().length() > 0) {
        crit.add(Restrictions.ilike("this.code", filterModel.getCode(), MatchMode.START));
    }/* w  w w . j ava  2 s. co m*/

    if (filterModel.getFullname() != null && filterModel.getFullname().length() > 0) {
        crit.add(Restrictions.ilike("this.fullname", filterModel.getFullname(), MatchMode.ANYWHERE));
    }

    if (filterModel.getCompany() != null && filterModel.getCompany().length() > 0) {
        crit.add(Restrictions.ilike("this.company", filterModel.getCompany(), MatchMode.START));
    }

    if (filterModel.getSsn() != null && filterModel.getSsn().length() > 0) {
        crit.add(Restrictions.ilike("this.ssn", filterModel.getSsn(), MatchMode.START));
    }

    if (filterModel.getTaxNumber() != null && filterModel.getTaxNumber().length() > 0) {
        crit.add(Restrictions.ilike("this.taxNumber", filterModel.getTaxNumber(), MatchMode.START));
    }

    if (filterModel.getRepresentative() != null && filterModel.getRepresentative().length() > 0) {
        crit.add(Restrictions.ilike("this.representative", filterModel.getRepresentative(), MatchMode.START));
    }

    if (filterModel.getExCode1() != null && filterModel.getExCode1().length() > 0) {
        crit.add(Restrictions.ilike("this.exCode1", filterModel.getExCode1(), MatchMode.START));
    }

    if (filterModel.getExCode2() != null && filterModel.getExCode2().length() > 0) {
        crit.add(Restrictions.ilike("this.exCode2", filterModel.getExCode2(), MatchMode.START));
    }

    if (filterModel.getCategory() != null) {
        crit.add(Restrictions.eq("this.category", filterModel.getCategory()));
    }

    if (filterModel.getOrganization() != null) {
        crit.add(Restrictions.eq("this.organization", filterModel.getOrganization()));
    }

    if (filterModel.getCompanyType() != null && !filterModel.getCompanyType().equals("All")) {
        if (filterModel.getCompanyType().equals("Person")) {
            crit.add(Restrictions.eq("this.person", Boolean.TRUE));
        } else
            crit.add(Restrictions.eq("this.person", Boolean.FALSE));
    }

    if (filterModel.getType() != null && filterModel.getType() != ContactType.All) {
        crit.add(Restrictions.eq("this." + filterModel.getType().toString().toLowerCase() + "Type",
                Boolean.TRUE));
    }

    if (filterModel.getCountry() != null) {
        crit.createAlias("this.addressList", "addressList", CriteriaSpecification.INNER_JOIN);
        crit.add(Restrictions.eq("addressList.address.country", filterModel.getCountry()));

        if (filterModel.getCity1() != null) {
            crit.add(Restrictions.eq("addressList.city", filterModel.getCity1()));
        }
    }

    //FIXME: bu kontrol nasl olmal?
    if (hasRegionRestriction()) {
        if (activeUser.getContact() != null && activeUser.getContact().getOrganization() != null
                && activeUser.getContact().getOrganization() != null) {

            crit.add(Restrictions.or(
                    Restrictions.eq("this.organization.id", activeUser.getContact().getOrganization().getId()),
                    Restrictions.eq("this.isPublic", Boolean.TRUE)));
        }
    }
    crit.addOrder(Order.desc("this.code"));

    return crit;
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleFormLayoutDao.java

License:Apache License

@Override
public int getFormsCount(Person owner) {
    DetachedCriteria criteria = DetachedCriteria.forClass(type);
    criteria.setProjection(Projections.distinct(Projections.countDistinct("formName")));
    if (owner != null)
        criteria.add(Restrictions.eq("person.personId", owner.getPersonId()));

    return DataAccessUtils.intResult(getHibernateTemplate().findByCriteria(criteria));
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleFormLayoutDao.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<String> getFormNames(Person owner) {
    DetachedCriteria criteria = DetachedCriteria.forClass(type);
    criteria.setProjection(Projections.distinct(Projections.property("formName")));
    if (owner != null)
        criteria.add(Restrictions.eq("person.personId", owner.getPersonId()));

    return getHibernateTemplate().findByCriteria(criteria);
}

From source file:data.dao.ColorGroupDao.java

public List<ColorGroup> findDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("oldGroupId")));
    return cr.list();
}

From source file:data.dao.FeatureDao.java

private List<Long> getCcoIdDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("ccoId")));
    cr.addOrder(Order.asc("ccoId"));
    return cr.list();
}

From source file:data.dao.FeatureDao.java

public List<Long> getCmgDict() {
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setProjection(Projections.distinct(Projections.property("cmgId")));
    cr.addOrder(Order.asc("cmgId"));
    return cr.list();
}

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

@SuppressWarnings("unchecked")
@Override//from  w  ww.  j ava  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.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);/*  w ww  .  j ava2 s . co  m*/
    crit.setResultTransformer(LocaleTupleResultTransformer.DEFAULT);
    return crit.list();
}

From source file:de.iteratec.iteraplan.businesslogic.reports.query.node.SealLeafNode.java

License:Open Source License

/**
 * Returns the Criterion for the outdated entities.
 * //ww  w. j  av  a2 s . c  o  m
 * @param effectivePropertyName the field name representing the seal state
 * @return the Criterion for the outdated entities
 */
private Criterion getOutdatedCriterion(String effectivePropertyName) {
    int expirationInDays = IteraplanProperties.getIntProperty(IteraplanProperties.SEAL_EXPIRATION_DAYS);
    Date minusDays = new DateTime().minusDays(expirationInDays).toDate();
    String idPropertyName = String.format("%s.%s", getResultTypeDBNameShortWithSuffix(), "id");

    final DetachedCriteria maxSealDate = DetachedCriteria.forClass(Seal.class, "seal");
    maxSealDate.setProjection(Projections.max("seal.date"));
    maxSealDate.add(Restrictions.eqProperty("seal.bb", idPropertyName));

    final DetachedCriteria lastSeal = DetachedCriteria.forClass(Seal.class, "lastSeal");
    lastSeal.add(Subqueries.propertyEq("lastSeal.date", maxSealDate));
    lastSeal.add(Restrictions.eqProperty("lastSeal.bb", idPropertyName));
    lastSeal.add(Restrictions.le("lastSeal.date", minusDays));
    lastSeal.setProjection(Projections.distinct(Property.forName("lastSeal.bb")));

    Criterion outdatedCriterion = Subqueries.propertyIn(idPropertyName, lastSeal);
    Criterion valid = Restrictions.eq(effectivePropertyName, SealState.VALID.toString());

    return Restrictions.and(valid, outdatedCriterion);
}

From source file:de.sub.goobi.forms.SearchForm.java

License:Open Source License

/**
 * Initialise drop down list of master piece property titles.
 *//*from  w ww  .ja  v  a  2s.  com*/
protected void initMasterpiecePropertyTitles() {
    Session session = Helper.getHibernateSession();
    Criteria crit = session.createCriteria(WorkpieceProperty.class);
    crit.addOrder(Order.asc("titel"));
    crit.setProjection(Projections.distinct(Projections.property("title")));
    this.masterpiecePropertyTitles.add(Helper.getTranslation("notSelected"));
    try {
        @SuppressWarnings("unchecked")
        List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list();
        for (String result : results) {
            this.masterpiecePropertyTitles.add(result);
        }
    } catch (HibernateException hbe) {
        logger.warn("Catched HibernateException. List of master piece property titles could be empty!");
    }
}