Example usage for org.hibernate.query Query setCacheable

List of usage examples for org.hibernate.query Query setCacheable

Introduction

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

Prototype

@Override
    Query<R> setCacheable(boolean cacheable);

Source Link

Usage

From source file:dk.fambagge.recipes.db.Database.java

public static <T extends DomainObject> T query(String hql, Class<T> aClass) {
    final List<T> namedResult = new LinkedList<>();

    execute((session) -> {/*from   www  .j  a  v  a  2  s .c  o  m*/
        Query query = session.createQuery(hql);
        query.setCacheable(true);

        namedResult.add((T) query.uniqueResult());
    });

    return namedResult.isEmpty() ? null : namedResult.get(0);
}

From source file:dk.fambagge.recipes.db.Database.java

public static <T extends DomainObject> Set<T> queryAll(String hql, Class<T> type, int firstResult,
        int maxResults) {
    final Set<T> namedResult = new LinkedHashSet<>();

    execute((session) -> {//from ww w . jav  a  2  s  .  c o m
        Query query = session.createQuery(hql);
        if (firstResult != -1) {
            query.setFirstResult(firstResult);
        }

        if (maxResults != -1) {
            query.setMaxResults(maxResults);
        }

        query.setCacheable(true);

        for (Object resultObj : query.list()) {
            namedResult.add((T) resultObj);
        }
    });

    return namedResult;
}

From source file:org.mycore.datamodel.classifications2.impl.MCRCategLinkServiceImpl.java

License:Open Source License

public Map<MCRCategoryID, Number> countLinksForType(MCRCategory parent, String type, boolean childrenOnly) {
    boolean restrictedByType = type != null;
    String queryName;/*from w  w w.  j av a  2s .  co  m*/
    if (childrenOnly) {
        queryName = restrictedByType ? "NumberByTypePerChildOfParentID" : "NumberPerChildOfParentID";
    } else {
        queryName = restrictedByType ? "NumberByTypePerClassID" : "NumberPerClassID";
    }
    Map<MCRCategoryID, Number> countLinks = new HashMap<MCRCategoryID, Number>();
    Collection<MCRCategoryID> ids = childrenOnly ? getAllChildIDs(parent) : getAllCategIDs(parent);
    for (MCRCategoryID id : ids) {
        // initialize all categIDs with link count of zero
        countLinks.put(id, 0);
    }
    //have to use rootID here if childrenOnly=false
    //old classification browser/editor could not determine links correctly otherwise 
    if (!childrenOnly) {
        parent = parent.getRoot();
    } else if (!(parent instanceof MCRCategoryImpl) || ((MCRCategoryImpl) parent).getInternalID() == 0) {
        parent = MCRCategoryDAOImpl.getByNaturalID(MCREntityManagerProvider.getCurrentEntityManager(),
                parent.getId());
    }
    LOGGER.info("parentID:" + parent.getId());
    String classID = parent.getId().getRootID();
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + queryName);
    // query can take long time, please cache result
    q.setCacheable(true);
    q.setParameter("classID", classID);
    if (childrenOnly) {
        q.setParameter("parentID", ((MCRCategoryImpl) parent).getInternalID());
    }
    if (restrictedByType) {
        q.setParameter("type", type);
    }
    // get object count for every category (not accumulated)
    @SuppressWarnings("unchecked")
    List<Object[]> result = (List<Object[]>) q.getResultList();
    for (Object[] sr : result) {
        MCRCategoryID key = new MCRCategoryID(classID, sr[0].toString());
        Number value = (Number) sr[1];
        countLinks.put(key, value);
    }
    return countLinks;
}

From source file:org.mycore.datamodel.classifications2.impl.MCRCategLinkServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public Collection<String> getLinksFromCategory(MCRCategoryID id) {
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + "ObjectIDByCategory");
    q.setCacheable(true);
    q.setParameter("id", id);
    return (Collection<String>) q.getResultList();
}

From source file:org.mycore.datamodel.classifications2.impl.MCRCategLinkServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public Collection<String> getLinksFromCategoryForType(MCRCategoryID id, String type) {
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + "ObjectIDByCategoryAndType");
    q.setCacheable(true);
    q.setParameter("id", id);
    q.setParameter("type", type);
    return (Collection<String>) q.getResultList();
}

From source file:org.mycore.datamodel.classifications2.impl.MCRCategLinkServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public Collection<MCRCategoryID> getLinksFromReference(MCRCategLinkReference reference) {
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + "categoriesByObjectID");
    q.setCacheable(true);
    q.setParameter("id", reference.getObjectID());
    q.setParameter("type", reference.getType());
    return (Collection<MCRCategoryID>) q.getResultList();
}

From source file:org.mycore.datamodel.classifications2.impl.MCRCategLinkServiceImpl.java

License:Open Source License

@Override
public boolean isInCategory(MCRCategLinkReference reference, MCRCategoryID id) {
    Query<?> q = HIB_CONNECTION_INSTANCE.getNamedQuery(NAMED_QUERY_NAMESPACE + "CategoryAndObjectID");
    q.setCacheable(true);
    q.setParameter("rootID", id.getRootID());
    q.setParameter("categID", id.getID());
    q.setParameter("objectID", reference.getObjectID());
    q.setParameter("type", reference.getType());
    return !q.getResultList().isEmpty();
}

From source file:org.openvpms.component.business.dao.hibernate.im.IMObjectDAOHibernate.java

License:Open Source License

/**
 * This method will execute a query and paginate the result set.
 *
 * @param queryString the hql query/*www. j  ava 2 s .c o  m*/
 * @param countQuery  the query to count results, or {@code null} if results are not being counted
 * @param params      the query parameters
 * @param collector   the collector
 * @param firstResult the first row to return
 * @param maxResults  the number of rows to return
 * @throws Exception for any error
 */
private void executeQuery(String queryString, String countQuery, Params params,
        HibernateResultCollector collector, int firstResult, int maxResults) throws Exception {
    Session session = getSession();
    collector.setFirstResult(firstResult);
    collector.setPageSize(maxResults);
    collector.setSession(session);
    Context context = getContext(session);
    collector.setContext(context);

    if (maxResults == 0 && countQuery != null) {
        // only want a count of the results matching the criteria
        int rowCount = count(countQuery, params, session);
        collector.setTotalResults(rowCount);
    } else {
        Query query = session.createQuery(queryString);
        params.setParameters(query);

        // set the first result
        if (firstResult != 0) {
            query.setFirstResult(firstResult);
        }

        // set the maximum number of rows
        if (maxResults != ArchetypeQuery.ALL_RESULTS) {
            query.setMaxResults(maxResults);
            log.debug("The maximum number of rows is " + maxResults);
        }

        query.setCacheable(true);

        List rows = query.list();
        if (maxResults == ArchetypeQuery.ALL_RESULTS) {
            collector.setTotalResults(rows.size());
        } else if (countQuery != null) {
            int rowCount = count(countQuery, params, session);
            if (rowCount < rows.size()) {
                // rows deleted since initial query
                rowCount = rows.size();
            }
            collector.setTotalResults(rowCount);
        } else {
            collector.setTotalResults(-1);
        }
        for (Object object : rows) {
            collector.collect(object);
        }
        context.resolveDeferredReferences();
    }
}