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.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

/**
 * Populates criteria arguments for the given target class and arguments map
 *
 * @param grailsApplication the GrailsApplication instance
 * @param targetClass The target class//from  w  ww  .  j  a  va2 s  . c o m
 * @param c The criteria instance
 * @param argMap The arguments map
 *
        
 */
@SuppressWarnings("rawtypes")
public static void populateArgumentsForCriteria(GrailsApplication grailsApplication, Class<?> targetClass,
        Criteria c, Map argMap) {
    Integer maxParam = null;
    Integer offsetParam = null;
    SimpleTypeConverter converter = new SimpleTypeConverter();
    if (argMap.containsKey(ARGUMENT_MAX)) {
        maxParam = converter.convertIfNecessary(argMap.get(ARGUMENT_MAX), Integer.class);
    }
    if (argMap.containsKey(ARGUMENT_OFFSET)) {
        offsetParam = converter.convertIfNecessary(argMap.get(ARGUMENT_OFFSET), Integer.class);
    }
    if (argMap.containsKey(ARGUMENT_FETCH_SIZE)) {
        c.setFetchSize(converter.convertIfNecessary(argMap.get(ARGUMENT_FETCH_SIZE), Integer.class));
    }
    if (argMap.containsKey(ARGUMENT_TIMEOUT)) {
        c.setTimeout(converter.convertIfNecessary(argMap.get(ARGUMENT_TIMEOUT), Integer.class));
    }
    if (argMap.containsKey(ARGUMENT_FLUSH_MODE)) {
        c.setFlushMode(converter.convertIfNecessary(argMap.get(ARGUMENT_FLUSH_MODE), FlushMode.class));
    }
    if (argMap.containsKey(ARGUMENT_READ_ONLY)) {
        c.setReadOnly(GrailsClassUtils.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 String sort = (String) argMap.get(ARGUMENT_SORT);
    final String order = ORDER_DESC.equalsIgnoreCase(orderParam) ? ORDER_DESC : ORDER_ASC;
    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 (GrailsClassUtils.getBooleanFromMap(ARGUMENT_CACHE, argMap)) {
        c.setCacheable(true);
    }
    if (GrailsClassUtils.getBooleanFromMap(ARGUMENT_LOCK, argMap)) {
        c.setLockMode(LockMode.PESSIMISTIC_WRITE);
    } else {
        if (argMap.get(ARGUMENT_CACHE) == null) {
            cacheCriteriaByMapping(targetClass, c);
        }
    }
    if (sort != null) {
        boolean ignoreCase = true;
        Object caseArg = argMap.get(ARGUMENT_IGNORE_CASE);
        if (caseArg instanceof Boolean) {
            ignoreCase = (Boolean) caseArg;
        }
        addOrderPossiblyNested(grailsApplication, c, targetClass, sort, order, ignoreCase);
    } else {
        Mapping m = GrailsDomainBinder.getMapping(targetClass);
        if (m != null && !StringUtils.isBlank(m.getSort())) {
            addOrderPossiblyNested(grailsApplication, c, targetClass, m.getSort(), m.getOrder(), true);
        }
    }
}

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

License:Apache License

/**
 * Configures the criteria instance to cache based on the configured mapping.
 *
 * @param targetClass The target class/*w ww . j av a  2s .com*/
 * @param criteria The criteria
 */
public static void cacheCriteriaByMapping(Class<?> targetClass, Criteria criteria) {
    Mapping m = GrailsDomainBinder.getMapping(targetClass);
    if (m != null && m.getCache() != null && m.getCache().getEnabled()) {
        criteria.setCacheable(true);
    }
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public MetadataField find(Context context, int metadataFieldId, MetadataSchema metadataSchema, String element,
        String qualifier) throws SQLException {
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.add(Restrictions.and(Restrictions.not(Restrictions.eq("id", metadataFieldId)),
            Restrictions.eq("metadataSchema.id", metadataSchema.getSchemaID()),
            Restrictions.eq("element", element), Restrictions.eqOrIsNull("qualifier", qualifier)));
    criteria.setCacheable(true);

    return singleResult(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public MetadataField findByElement(Context context, MetadataSchema metadataSchema, String element,
        String qualifier) throws SQLException {
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.add(Restrictions.and(Restrictions.eq("metadataSchema.id", metadataSchema.getSchemaID()),
            Restrictions.eq("element", element), Restrictions.eqOrIsNull("qualifier", qualifier)));
    criteria.setCacheable(true);

    return singleResult(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public List<MetadataField> findAll(Context context, Class<MetadataField> clazz) throws SQLException {
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.createAlias("metadataSchema", "s").addOrder(Order.asc("s.name")).addOrder(Order.asc("element"))
            .addOrder(Order.asc("qualifier"));
    criteria.setCacheable(true);
    return list(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public MetadataField findByElement(Context context, String metadataSchema, String element, String qualifier)
        throws SQLException {
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.createAlias("metadataSchema", "s").add(Restrictions.and(Restrictions.eq("s.name", metadataSchema),
            Restrictions.eq("element", element), Restrictions.eqOrIsNull("qualifier", qualifier)));
    criteria.setCacheable(true);

    return singleResult(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public List<MetadataField> findFieldsByElementNameUnqualified(Context context, String metadataSchema,
        String element) throws SQLException {
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.createAlias("metadataSchema", "s").add(
            Restrictions.and(Restrictions.eq("s.name", metadataSchema), Restrictions.eq("element", element)));
    criteria.setCacheable(true);

    return list(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataFieldDAOImpl.java

License:BSD License

@Override
public List<MetadataField> findAllInSchema(Context context, MetadataSchema metadataSchema) throws SQLException {
    // Get all the metadatafieldregistry rows
    Criteria criteria = createCriteria(context, MetadataField.class);
    criteria.createAlias("metadataSchema", "s");
    criteria.add(Restrictions.eq("s.id", metadataSchema.getSchemaID()));
    criteria.addOrder(Order.asc("s.name")).addOrder(Order.asc("element")).addOrder(Order.asc("qualifier"));

    criteria.setCacheable(true);
    return list(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataSchemaDAOImpl.java

License:BSD License

/**
 * Get the schema object corresponding to this namespace URI.
 *
 * @param context DSpace context/*from w w w.java  2  s. c  o  m*/
 * @param namespace namespace URI to match
 * @return metadata schema object or null if none found.
 * @throws java.sql.SQLException
 */
@Override
public MetadataSchema findByNamespace(Context context, String namespace) throws SQLException {
    // Grab rows from DB
    Criteria criteria = createCriteria(context, MetadataSchema.class);
    criteria.add(Restrictions.eq("namespace", namespace));
    criteria.setCacheable(true);

    return uniqueResult(criteria);
}

From source file:org.dspace.content.dao.impl.MetadataSchemaDAOImpl.java

License:BSD License

@Override
public List<MetadataSchema> findAll(Context context, Class clazz) throws SQLException {
    // Get all the metadataschema rows
    Criteria criteria = createCriteria(context, MetadataSchema.class);
    criteria.addOrder(Order.asc("id"));
    criteria.setCacheable(true);

    return list(criteria);
}