Example usage for org.hibernate Criteria setCacheable

List of usage examples for org.hibernate Criteria setCacheable

Introduction

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

Prototype

public Criteria setCacheable(boolean cacheable);

Source Link

Document

Enable caching of this query result, provided query caching is enabled for the underlying session factory.

Usage

From source file:org.gbif.portal.dao.resources.impl.hibernate.DataProviderDAOImpl.java

License:Open Source License

/**
 * @see org.gbif.portal.dao.resources.DataProviderDAO#findDataProviders(java.lang.String, boolean, int, int)
 *///from   w w  w  .  j a va  2 s  . c  o  m
@SuppressWarnings("unchecked")
public List<DataProvider> findDataProviders(final String nameStub, final boolean fuzzy,
        final String isoCountryCode, final Date modifiedSince, final int startIndex, final int maxResults) {
    HibernateTemplate template = getHibernateTemplate();
    return (List<DataProvider>) template.execute(new HibernateCallback() {

        public Object doInHibernate(Session session) {
            Criteria criteria = session.createCriteria(DataProvider.class, "dp");
            if (nameStub != null) {
                if (fuzzy) {
                    criteria = criteria.add(Restrictions.like("dp.name", nameStub, MatchMode.START));
                } else {
                    criteria = criteria.add(Restrictions.eq("dp.name", nameStub));
                }
            }
            if (isoCountryCode != null) {
                criteria = criteria.add(Restrictions.eq("dp.isoCountryCode", isoCountryCode));
            }
            if (modifiedSince != null) {
                criteria = criteria.add(Restrictions.ge("dp.modified", modifiedSince));
            }
            criteria.setCacheable(true);
            return criteria.list();
        }
    });
}

From source file:org.gbif.portal.dao.resources.impl.hibernate.DataResourceDAOImpl.java

License:Open Source License

/**
 * @see org.gbif.portal.dao.resources.DataResourceDAO#findDataResources(java.lang.String, boolean, java.lang.Integer,
 *      java.lang.Integer)//from w ww . ja v a  2  s  .c om
 */
@SuppressWarnings("unchecked")
public List<DataResource> findDataResources(final String nameStub, final boolean fuzzy,
        final DataProvider dataProvider, final BasisOfRecord basisOfRecord, final Date modifiedSince,
        final int startIndex, final int maxResults) {
    HibernateTemplate template = getHibernateTemplate();
    return (List<DataResource>) template.execute(new HibernateCallback() {

        public Object doInHibernate(Session session) {
            Criteria criteria = session.createCriteria(DataResource.class, "dr");
            if (nameStub != null) {
                if (fuzzy) {
                    criteria = criteria.add(Restrictions.like("dr.name", nameStub, MatchMode.START));
                } else {
                    criteria = criteria.add(Restrictions.eq("dr.name", nameStub));
                }
            }
            if (dataProvider != null) {
                criteria = criteria.add(Restrictions.eq("dr.dataProvider", dataProvider));
            }
            if (basisOfRecord != null) {
                criteria = criteria.add(Restrictions.eq("dr.basisOfRecord", basisOfRecord));
            }
            if (modifiedSince != null) {
                criteria = criteria.add(Restrictions.ge("dp.modified", modifiedSince));
            }
            criteria = criteria.add(Restrictions.isNull("dr.deleted"));
            criteria.setFetchMode("dr.dataProvider", FetchMode.JOIN);
            criteria.setCacheable(true);
            criteria.setMaxResults(maxResults);
            criteria.setFirstResult(startIndex);
            return criteria.list();
        }
    });
}

From source file:org.gbif.portal.dao.resources.impl.hibernate.ResourceNetworkDAOImpl.java

License:Open Source License

/**
 * @see org.gbif.portal.dao.resources.ResourceNetworkDAO#findResourceNetworks(java.lang.String, boolean, int, int)
 *//* w ww. j  a v  a2  s. c  o  m*/
@SuppressWarnings("unchecked")
public List<ResourceNetwork> findResourceNetworks(final String nameStub, final boolean fuzzy, final String code,
        final Date modifiedSince, final int startIndex, final int maxResults) {
    HibernateTemplate template = getHibernateTemplate();
    return (List<ResourceNetwork>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Criteria criteria = session.createCriteria(ResourceNetwork.class, "rn");
            if (nameStub != null) {
                if (fuzzy) {
                    criteria = criteria.add(Restrictions.like("rn.name", nameStub, MatchMode.START));
                } else {
                    criteria = criteria.add(Restrictions.eq("rn.name", nameStub));
                }
            }
            if (code != null) {
                criteria = criteria.add(Restrictions.eq("rn.code", code));
            }
            if (modifiedSince != null) {
                criteria = criteria.add(Restrictions.ge("rn.modified", modifiedSince));
            }
            criteria.setCacheable(true);
            return criteria.list();
        }
    });
}

From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

/**
 * Populates criteria arguments for the given target class and arguments map
 *
 * @param datastore the GrailsApplication instance
 * @param targetClass The target class//from  w w w  . jav  a2  s  .  c om
 * @param c The criteria instance
 * @param argMap The arguments map
 */
@SuppressWarnings("rawtypes")
public static void populateArgumentsForCriteria(AbstractHibernateDatastore datastore, Class<?> targetClass,
        Criteria c, Map argMap, ConversionService conversionService, boolean useDefaultMapping) {
    Integer maxParam = null;
    Integer offsetParam = null;
    if (argMap.containsKey(ARGUMENT_MAX)) {
        maxParam = conversionService.convert(argMap.get(ARGUMENT_MAX), Integer.class);
    }
    if (argMap.containsKey(ARGUMENT_OFFSET)) {
        offsetParam = conversionService.convert(argMap.get(ARGUMENT_OFFSET), Integer.class);
    }
    if (argMap.containsKey(ARGUMENT_FETCH_SIZE)) {
        c.setFetchSize(conversionService.convert(argMap.get(ARGUMENT_FETCH_SIZE), Integer.class));
    }
    if (argMap.containsKey(ARGUMENT_TIMEOUT)) {
        c.setTimeout(conversionService.convert(argMap.get(ARGUMENT_TIMEOUT), Integer.class));
    }
    if (argMap.containsKey(ARGUMENT_FLUSH_MODE)) {
        c.setFlushMode(convertFlushMode(argMap.get(ARGUMENT_FLUSH_MODE)));
    }
    if (argMap.containsKey(ARGUMENT_READ_ONLY)) {
        c.setReadOnly(ClassUtils.getBooleanFromMap(ARGUMENT_READ_ONLY, argMap));
    }
    String orderParam = (String) argMap.get(ARGUMENT_ORDER);
    Object fetchObj = argMap.get(ARGUMENT_FETCH);
    if (fetchObj instanceof Map) {
        Map fetch = (Map) fetchObj;
        for (Object o : fetch.keySet()) {
            String associationName = (String) o;
            c.setFetchMode(associationName, getFetchMode(fetch.get(associationName)));
        }
    }

    final int max = maxParam == null ? -1 : maxParam;
    final int offset = offsetParam == null ? -1 : offsetParam;
    if (max > -1) {
        c.setMaxResults(max);
    }
    if (offset > -1) {
        c.setFirstResult(offset);
    }
    if (ClassUtils.getBooleanFromMap(ARGUMENT_LOCK, argMap)) {
        c.setLockMode(LockMode.PESSIMISTIC_WRITE);
        c.setCacheable(false);
    } else {
        if (argMap.containsKey(ARGUMENT_CACHE)) {
            c.setCacheable(ClassUtils.getBooleanFromMap(ARGUMENT_CACHE, argMap));
        } else {
            cacheCriteriaByMapping(targetClass, c);
        }
    }

    final Object sortObj = argMap.get(ARGUMENT_SORT);
    if (sortObj != null) {
        boolean ignoreCase = true;
        Object caseArg = argMap.get(ARGUMENT_IGNORE_CASE);
        if (caseArg instanceof Boolean) {
            ignoreCase = (Boolean) caseArg;
        }
        if (sortObj instanceof Map) {
            Map sortMap = (Map) sortObj;
            for (Object sort : sortMap.keySet()) {
                final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC
                        : ORDER_ASC;
                addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, ignoreCase);
            }
        } else {
            final String sort = (String) sortObj;
            final String order = ORDER_DESC.equalsIgnoreCase(orderParam) ? ORDER_DESC : ORDER_ASC;
            addOrderPossiblyNested(datastore, c, targetClass, sort, order, ignoreCase);
        }
    } else if (useDefaultMapping) {
        Mapping m = GrailsDomainBinder.getMapping(targetClass);
        if (m != null) {
            Map sortMap = m.getSort().getNamesAndDirections();
            for (Object sort : sortMap.keySet()) {
                final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC
                        : ORDER_ASC;
                addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, true);
            }
        }
    }
}

From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or a
 * transaction timeout./*from   www.j a  v  a2s  . co  m*/
 *
 * @param criteria the Criteria object to prepare
 */
protected void prepareCriteria(Criteria criteria) {
    if (cacheQueries) {
        criteria.setCacheable(true);
    }
    if (shouldPassReadOnlyToHibernate()) {
        criteria.setReadOnly(true);
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

From source file:org.iesc.flightws.domain.BookingDaoBase.java

/**
 * @see org.iesc.flightws.domain.BookingDao#loadAll(int, int, int)
 */// w w w. j  a v  a 2 s .  c  o m
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.BookingImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.CityDaoBase.java

/**
 * @see org.iesc.flightws.domain.CityDao#loadAll(int, int, int)
 *//*from   ww  w  .j  a v  a2 s  .c  om*/
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.CityImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.FlightDaoBase.java

/**
 * @see org.iesc.flightws.domain.FlightDao#loadAll(int, int, int)
 *//*  ww w . j  a  v a 2 s.c  o  m*/
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.FlightImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.FlightDateDaoBase.java

/**
 * @see org.iesc.flightws.domain.FlightDateDao#loadAll(int, int, int)
 *//*from   ww  w . j a  va 2  s  .co  m*/
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.FlightDateImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.PassengerDaoBase.java

/**
 * @see org.iesc.flightws.domain.PassengerDao#loadAll(int, int, int)
 *//*from  www  . j  av  a2 s . co m*/
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.PassengerImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}