Example usage for org.hibernate Criteria setFlushMode

List of usage examples for org.hibernate Criteria setFlushMode

Introduction

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

Prototype

public Criteria setFlushMode(FlushMode flushMode);

Source Link

Document

Override the flush mode for this particular query.

Usage

From source file:org.openspaces.persistency.hibernate.iterator.DefaultScrollableDataIterator.java

License:Open Source License

protected ScrollableResults createCursor() {
    session = sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setCacheable(false);/*from ww  w . jav  a 2s .  c o m*/
        criteria.setFlushMode(FlushMode.MANUAL);
        criteria.setFetchSize(fetchSize);
        if (perfromOrderById) {
            ClassMetadata metadata = sessionFactory.getClassMetadata(entityName);
            String idPropName = metadata.getIdentifierPropertyName();
            if (idPropName != null) {
                criteria.addOrder(Order.asc(idPropName));
            }
        }
        if (from >= 0) {
            if (from > 0)
                criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.scroll(ScrollMode.FORWARD_ONLY);
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else if (hQuery != null) {
        Query query = session.createQuery(hQuery);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        query.setCacheMode(CacheMode.IGNORE);
        query.setCacheable(false);
        query.setReadOnly(true);
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else {
        throw new IllegalStateException("No SQLQuery, entity, or Hibernate Query provided");
    }
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> List<E> findByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setCacheable(query.isCached());

    //Issue #21/*from  w  w  w  .j av a2 s. co  m*/
    criteria.setFlushMode(FlushMode.MANUAL);

    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }
    return criteria.list();
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findFirstByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(1).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }//  w ww .j  ava  2s . c om

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() > 0) {
        return list.get(0);
    }
    return null;
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findUniqueByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(2).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }//www .j  a v  a2s . co m

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() == 0) {
        return null;
    } else if (list.size() == 1) {
        return list.get(0);
    } else {
        throw new DataIntegrityViolationException("expecting unique result but got many");
    }
}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.BioAssayDimensionDaoImpl.java

License:Apache License

@Override
public BioAssayDimension find(BioAssayDimension bioAssayDimension) {

    if (bioAssayDimension.getBioAssays().isEmpty()) {
        throw new IllegalArgumentException("BioAssayDimension had no BioAssays");
    }//  w  ww .  j  a  va2 s .co m

    Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(BioAssayDimension.class);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    if (StringUtils.isNotBlank(bioAssayDimension.getName())) {
        queryObject.add(Restrictions.eq("name", bioAssayDimension.getName()));
    }

    if (StringUtils.isNotBlank(bioAssayDimension.getDescription())) {
        queryObject.add(Restrictions.eq("description", bioAssayDimension.getDescription()));
    }

    queryObject.add(Restrictions.sizeEq("bioAssays", bioAssayDimension.getBioAssays().size()));

    Collection<String> names = new HashSet<>();
    for (BioAssay bioAssay : bioAssayDimension.getBioAssays()) {
        names.add(bioAssay.getName());
    }
    queryObject.createCriteria("bioAssays").add(Restrictions.in("name", names));

    BioAssayDimension candidate = (BioAssayDimension) queryObject.uniqueResult();

    if (candidate == null)
        return null;

    // Now check that the bioassays and order are exactly the same.
    Collection<BioAssay> desiredBioAssays = bioAssayDimension.getBioAssays();
    Collection<BioAssay> candidateBioAssays = candidate.getBioAssays();

    assert desiredBioAssays.size() == candidateBioAssays.size();

    Iterator<BioAssay> dit = desiredBioAssays.iterator();
    Iterator<BioAssay> cit = candidateBioAssays.iterator();

    while (dit.hasNext()) {
        BioAssay d = dit.next();
        BioAssay c = cit.next();
        if (!c.equals(d))
            return null;
    }

    return candidate;

}

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

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from www. ja  v  a 2s.  c  om*/
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   www  .  ja v a2s. 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;

}