List of usage examples for javax.persistence.criteria CriteriaBuilder in
<T> In<T> in(Expression<? extends T> expression);
From source file:com.goodhuddle.huddle.repository.BlogPostSpecification.java
public static Specification<BlogPost> search(final Huddle huddle, final SearchBlogPostRequest request) { return new Specification<BlogPost>() { @Override/* w ww.j a v a2 s . com*/ public Predicate toPredicate(Root<BlogPost> blogPost, CriteriaQuery<?> query, CriteriaBuilder builder) { Predicate conjunction = builder.conjunction(); conjunction.getExpressions().add(builder.equal(blogPost.get("huddle"), huddle)); if (StringUtils.isNotBlank(request.getPhrase())) { String phrase = "%" + request.getPhrase().toLowerCase() + "%"; conjunction.getExpressions() .add(builder.like(builder.lower(blogPost.<String>get("title")), phrase)); } if (CollectionUtils.isNotEmpty(request.getBlogIds())) { Join<Object, Object> blog = blogPost.join("blog"); conjunction.getExpressions().add(builder.in(blog.get("id")).value(request.getBlogIds())); } if (!request.isIncludeUnpublished()) { conjunction.getExpressions() .add(builder.lessThan((Expression) blogPost.get("publishedOn"), new DateTime())); } return conjunction; } }; }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
private <T extends ResourceEntity> long getTotalResults(Class<T> clazz, Subquery<Long> internalIdQuery) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> resourceQuery = cb.createQuery(Long.class); Root<T> resourceRoot = resourceQuery.from(clazz); resourceQuery.select(cb.count(resourceRoot)) .where(cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery)); Long total = em.createQuery(resourceQuery).getSingleResult(); return total; }
From source file:com.expressui.sample.view.account.AccountQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Account> rootEntity) { List<Predicate> criteria = new ArrayList<Predicate>(); if (!isEmpty(name)) { ParameterExpression<String> p = builder.parameter(String.class, "name"); criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p)); }/*from w w w . j av a 2s. c om*/ if (!isEmpty(states)) { ParameterExpression<Set> p = builder.parameter(Set.class, "states"); criteria.add(builder.in(rootEntity.get("billingAddress").get("state")).value(p)); } if (!isEmpty(country)) { ParameterExpression<Country> p = builder.parameter(Country.class, "country"); criteria.add(builder.equal(rootEntity.get("billingAddress").get("country"), p)); } return criteria; }
From source file:com.expressui.sample.view.contact.ContactQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Contact> rootEntity) { List<Predicate> criteria = new ArrayList<Predicate>(); if (!isEmpty(lastName)) { ParameterExpression<String> p = builder.parameter(String.class, "lastName"); criteria.add(builder.like(builder.upper(rootEntity.<String>get("lastName")), p)); }/*from ww w. j a v a 2s . com*/ if (!isEmpty(states)) { ParameterExpression<Set> p = builder.parameter(Set.class, "states"); criteria.add(builder.in(rootEntity.get("mailingAddress").get("state")).value(p)); } if (!isEmpty(country)) { ParameterExpression<Country> p = builder.parameter(Country.class, "country"); criteria.add(builder.equal(rootEntity.get("mailingAddress").get("country"), p)); } return criteria; }
From source file:com.expressui.sample.view.opportunity.OpportunityQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Opportunity> rootEntity) { List<Predicate> criteria = new ArrayList<Predicate>(); if (!isEmpty(accountName)) { ParameterExpression<String> p = builder.parameter(String.class, "accountName"); criteria.add(builder.like(builder.upper(rootEntity.get("account").<String>get("name")), p)); }//from w w w . j a v a2 s . c om if (!isEmpty(salesStages)) { ParameterExpression<Set> p = builder.parameter(Set.class, "salesStages"); criteria.add(builder.in(rootEntity.get("salesStage")).value(p)); } return criteria; }
From source file:com.expressui.sample.dao.query.AccountQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Account> query, Root<Account> account) {//www . j a va 2 s . c o m List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(name)) { ParameterExpression<String> nameExp = builder.parameter(String.class, "name"); predicates.add(builder.like(builder.upper(account.<String>get("name")), nameExp)); } if (hasValue(states)) { ParameterExpression<Set> statesExp = builder.parameter(Set.class, "states"); predicates.add(builder.in(account.get("billingAddress").get("state")).value(statesExp)); } if (hasValue(country)) { ParameterExpression<Country> countryExp = builder.parameter(Country.class, "country"); predicates.add(builder.equal(account.get("billingAddress").get("country"), countryExp)); } return predicates; }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * in and path:/* w w w.j av a2 s.c o m*/ * SELECT e FROM jpa_query_employee e WHERE e.address.state IN ('NY','MI') */ @Transactional public void doWhere3() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // method 1 c.select(e).where(e.get("address").get("state").in("NY", "MI")); showResult(c); // method 2 c.select(e).where(cb.in(e.get("address").get("state")).value("NY").value("MI")); showResult(c); }
From source file:com.expressui.sample.dao.query.OpportunityQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Opportunity> query, Root<Opportunity> opportunity) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(accountName)) { ParameterExpression<String> accountNameExp = builder.parameter(String.class, "accountName"); predicates.add(//from www .j a v a 2 s . co m builder.like(builder.upper(opportunity.get("account").<String>get("name")), accountNameExp)); } if (hasValue(salesStages)) { ParameterExpression<Set> salesStagesExp = builder.parameter(Set.class, "salesStages"); predicates.add(builder.in(opportunity.get("salesStage")).value(salesStagesExp)); } return predicates; }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional//from w w w .ja v a2 s.c o m public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java
@Override public Specification<Device> forOrganisation(final Organisation organisation) throws ArgumentNullOrEmptyException { if (organisation == null) { throw new ArgumentNullOrEmptyException("organisation"); }//from w w w . j a v a 2 s . c om return new Specification<Device>() { @Override public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Subquery<Long> subquery = query.subquery(Long.class); final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class); subquery.select(deviceAuthorizationRoot.get("device").get("id").as(Long.class)); subquery.where(cb.equal(deviceAuthorizationRoot.get("organisation"), organisation.getId())); return cb.in(deviceRoot.get("id")).value(subquery); } }; }