Example usage for org.hibernate Criteria setReadOnly

List of usage examples for org.hibernate Criteria setReadOnly

Introduction

In this page you can find the example usage for org.hibernate Criteria setReadOnly.

Prototype

public Criteria setReadOnly(boolean readOnly);

Source Link

Document

Set the read-only/modifiable mode for entities and proxies loaded by this Criteria.

Usage

From source file:ubic.gemma.persistence.service.genome.biosequence.BioSequenceDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   ww w. ja v  a 2  s.c o m
public BioSequence find(BioSequence bioSequence) {

    BusinessKey.checkValidKey(bioSequence);

    Criteria queryObject = BusinessKey.createQueryObject(this.getSessionFactory().getCurrentSession(),
            bioSequence);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);
    /*
     * this initially matches on name and taxon only.
     */
    java.util.List<?> results = queryObject.list();
    Object result = null;
    if (results != null) {
        if (results.size() > 1) {
            this.debug(bioSequence, results);

            // Try to find the best match. See BusinessKey for more
            // explanation of why this is needed.
            BioSequence match = null;
            for (BioSequence res : (Collection<BioSequence>) results) {
                if (res.equals(bioSequence)) {
                    if (match != null) {
                        AbstractDao.log.warn("More than one sequence in the database matches " + bioSequence
                                + ", returning arbitrary match: " + match);
                        break;
                    }
                    match = res;
                }
            }

            return match;

        } else if (results.size() == 1) {
            result = results.iterator().next();
        }
    }
    return (BioSequence) result;
}

From source file:ubic.gemma.persistence.service.genome.taxon.TaxonDaoImpl.java

License:Apache License

@Override
public Taxon find(Taxon taxon) {

    BusinessKey.checkValidKey(taxon);//from   w  w w .ja  v a  2s.c o m

    Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(Taxon.class)
            .setReadOnly(true);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);
    BusinessKey.addRestrictions(queryObject, taxon);

    List<?> results = queryObject.list();
    Object result = null;
    if (results != null) {
        if (results.size() > 1) {
            throw new org.springframework.dao.InvalidDataAccessResourceUsageException(
                    "More than one instance of '" + taxon.getClass().getName()
                            + "' was found when executing query");
        } else if (results.size() == 1) {
            result = results.iterator().next();
        }
    }
    return (Taxon) result;

}