This is a way to create and execute JCR queries using Java code, using an interface which was inspired by the Criteria API as used by Hibernate/JPA. This code is based on the Criteria API for Magnolia CMS (openutils-mgnlcriteria) module which was developed by Openmind.
In contrast to openutils-mgnlcriteria
there is no dependency on any Magnolia CMS code in jcr-criteria
, so this is a generic JCR Criteria API implementation. It can still be used with Magnolia CMS, but should work with other JCR-based projects as well.
You can download the most recent jar from https://oss.sonatype.org/content/repositories/snapshots/nl/vpro/jcr-criteria/ or you can add this to your pom.xml
:
<dependency>
<groupId>nl.vpro</groupId>
<artifactId>jcr-criteria</artifactId>
<version>2.11</version>
</dependency>
Example:
import nl.vpro.jcr.criteria.query.AdvancedResult;
import nl.vpro.jcr.criteria.query.AdvancedResultItem;
import nl.vpro.jcr.criteria.query.Criteria;
import nl.vpro.jcr.criteria.query.JCRCriteriaFactory;
import nl.vpro.jcr.criteria.query.criterion.Criterion;
import nl.vpro.jcr.criteria.query.criterion.Restrictions;
..
Criteria criteria = JCRCriteriaFactory.createCriteria()
.setBasePath(basePath)
.setPaging(1, 1)
.addOrder(Order.ascending(field))
.add(Restrictions.eq(Criterion.JCR_PRIMARYTYPE, NodeTypes.Page.NAME))
.add(Restrictions.in("@" + NodeTypes.Renderable.TEMPLATE, templates))
.add(Restrictions.gt(field, begin));
AdvancedResult ar = criteria.execute(MgnlContext.getJCRSession(RepositoryConstants.WEBSITE));
log.debug("JCR query : " + criteria.toXpathExpression());
AdvancedResultItem item = ar.getFirstResult();
It can also be done, if you prefer, using the builder pattern
static import nl.vpro.jcr.criteria.query.criterion.Restrictions.*;
ExecutableQuery criteria = JCRCriteriaFactory.builder()
.basePath(basePath)
.order(Order.ascending(field))
.add(eq(Criterion.JCR_PRIMARYTYPE, NodeTypes.Page.NAME))
.add(in(attr(NodeTypes.Renderable.TEMPLATE), templates))
.add(gt(field, begin))
.build()
;
AdvancedResult result = criteria.execute(session);