0

I am using Java8 and JPA2.

I would like to add a predicate to a where clause.

I have the following code:

@Override
public List<Job> findById(String ids, Integer type) {

    String[] args = ids.split(",");
    Set<String> idSet = new HashSet<String>(Arrays.asList(args));

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Job> query = builder.createQuery(Job.class);
    Root<Job> post = query.from(Job.class);
    CriteriaQuery<Job> criteria = query.select(post);
    criteria.where(post.get("id").in(idSet));
    //criteria.where(post.get("type").in(type));  <=== how do I do an equals
    TypedQuery<Job> typedQuery = entityManager.createQuery(query);
    List<Job> results = typedQuery.getResultList();
    return results;
}

As you can see, I use a criteria object and have a where clause that filters on "ids" in the id set.

I would like to add an addition filter to the where clause, but am not sure how to. I would like to add a clause where column "type" equals the value in the type Integer object.

i.e.

where id in [...] and type = 1

Any advise appreciated.

Richard
  • 8,193
  • 28
  • 107
  • 228
  • how to what? have 2 where clauses? pass both in to the where() method. Or use _builder.and_. If you mean the equals part ... _builder.equal()_ – Neil Stockton Nov 04 '16 at 09:26
  • Asked and answered before: http://stackoverflow.com/a/12199937/212224 a quick google search came up with a lot of examples of different ways to achieve the same thing. – Mike Nov 04 '16 at 09:26
  • Possible duplicate of [JPA Criteria API with multiple parameters](http://stackoverflow.com/questions/12199433/jpa-criteria-api-with-multiple-parameters) – Mike Nov 04 '16 at 09:27
  • Look at http://stackoverflow.com/questions/40397347/jpa-fully-dynamic-and-multi-level-criteria-api – Essex Boy Nov 04 '16 at 10:06

0 Answers0