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:ubic.gemma.persistence.service.genome.gene.GeneProductDaoImpl.java

License:Apache License

@Override
public GeneProduct find(GeneProduct geneProduct) {
    try {//  w w w  .  j  a  v a 2s . co m
        Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(GeneProduct.class)
                .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        BusinessKey.checkValidKey(geneProduct);

        BusinessKey.createQueryObject(queryObject, geneProduct);

        AbstractDao.log.debug(queryObject);

        //noinspection unchecked
        List<GeneProduct> results = queryObject.list();
        Object result = null;
        if (results.size() > 1) {

            /*
             * At this point we can trust that the genes are from the same taxon. This kind of confusion should
             * reduce with cruft-reduction.
             */
            Collections.sort(results, GeneProductDaoImpl.c); // we tend to want to keep the one with the lowest ID
            Gene gene = geneProduct.getGene();
            if (gene != null) {
                GeneProduct keeper = null;
                int numFound = 0;
                for (Object object : results) {
                    GeneProduct candidateMatch = (GeneProduct) object;

                    Gene candidateGene = candidateMatch.getGene();
                    if (candidateGene.getOfficialSymbol().equals(gene.getOfficialSymbol())
                            && candidateGene.getTaxon().equals(gene.getTaxon())) {
                        keeper = candidateMatch;
                        numFound++;
                    }
                }

                if (numFound == 1) {
                    // not so bad, we figured out a match.
                    AbstractDao.log.warn("Multiple gene products match " + geneProduct
                            + ", but only one for the right gene (" + gene + "), returning " + keeper);
                    this.debug(results);
                    return keeper;
                }

                if (numFound == 0) {
                    AbstractDao.log
                            .error("Multiple gene products match " + geneProduct + ", but none with " + gene);
                    this.debug(results);
                    AbstractDao.log.error("Returning arbitrary match " + results.iterator().next());
                    return results.iterator().next();
                }

                if (numFound > 1) {
                    AbstractDao.log.error("Multiple gene products match " + geneProduct + ", and matches "
                            + numFound + " genes");
                    this.debug(results);
                    AbstractDao.log.error("Returning arbitrary match " + results.iterator().next());
                    return results.iterator().next();
                }
            }

        } else if (results.size() == 1) {
            result = results.iterator().next();
        }
        if (result == null)
            return null;
        AbstractDao.log.debug("Found: " + result);
        return (GeneProduct) result;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:ubic.gemma.persistence.util.BusinessKey.java

License:Apache License

public static void createQueryObject(Criteria queryObject, FactorValue factorValue) {

    ExperimentalFactor ef = factorValue.getExperimentalFactor();

    if (ef == null)
        throw new IllegalArgumentException("Must have experimentalfactor on factorvalue to search");

    Criteria innerQuery = queryObject.createCriteria("experimentalFactor");
    BusinessKey.addRestrictions(innerQuery, ef);

    if (factorValue.getValue() != null) {
        queryObject.add(Restrictions.eq("value", factorValue.getValue()));
    } else if (factorValue.getCharacteristics().size() > 0) {

        /*/*from   ww w .  j a  v  a 2 s.c o m*/
         * All the characteristics have to match ones in the result, and the result cannot have any extras. In other
         * words there has to be a one-to-one match between the characteristics.
         */

        // this takes care of the size check
        queryObject.add(Restrictions.sizeEq("characteristics", factorValue.getCharacteristics().size()));

        // now the equivalence.
        Criteria characteristicsCriteria = queryObject.createCriteria("characteristics");

        /*
         * Note that this isn't exactly correct, but it should work okay: "If all the characteristics in the
         * candidate are also in the query", along with the size restriction. The only problem would be if the same
         * characteristic were added to an object more than once - so the sizes would be the same, but a
         * characteristic in the query might not show up in the candidate. Multiple entries of the same
         * characteristic shouldn't be allowed, and even if it did happen the chance of a problem is small.... but a
         * formal possibility.
         */
        Disjunction vdj = Restrictions.disjunction();
        for (Characteristic characteristic : factorValue.getCharacteristics()) {

            Conjunction c = Restrictions.conjunction();

            if (StringUtils.isNotBlank(characteristic.getCategoryUri())) {
                c.add(Restrictions.eq("categoryUri", characteristic.getCategoryUri()));
            }
            if (StringUtils.isNotBlank(characteristic.getValueUri())) {
                c.add(Restrictions.eq("valueUri", characteristic.getValueUri()));
            }

            if (StringUtils.isNotBlank(characteristic.getValue()))
                c.add(Restrictions.eq("value", characteristic.getValue()));

            if (StringUtils.isNotBlank(characteristic.getCategory()))
                c.add(Restrictions.eq("category", characteristic.getCategory()));

            vdj.add(c);
        }
        characteristicsCriteria.add(vdj);

    } else if (factorValue.getMeasurement() != null) {
        queryObject.add(Restrictions.eq("measurement", factorValue.getMeasurement()));
    }

    queryObject.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
}