Question: JPA CriteriaBuilder - How to use ?IN? comparison operator

Ads

 
 
 

Share on Google+Share on Google+

jem
JPA CriteriaBuilder - How to use ?IN? comparison operator
0 Answer(s)      5 years and 3 months ago
Posted in : Hibernate

Can you please help me how to convert the following codes to using "in" operator of criteria builder? I need to filter by using list/array of usernames using "in".

***I also tried to search using JPA CriteriaBuilder - "in" method but cannot find good result. So I would really appreciate also if you can give me reference URLs for this topic. Thanks.***

Here is my codes:

    //usersList is a list of User that I need to put inside IN operator 

    CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder();
    CriteriaQuery<ScheduleRequest> criteria = builder.createQuery(ScheduleRequest.class);

    Root<ScheduleRequest> scheduleRequest = criteria.from(ScheduleRequest.class);
    criteria = criteria.select(scheduleRequest);

    List<Predicate> params = new ArrayList<Predicate>();

    List<ParameterExpression<String>> usersIdsParamList = new ArrayList<ParameterExpression<String>>();

    for (int i = 0; i < usersList.size(); i++) {
    ParameterExpression<String> usersIdsParam = builder.parameter(String.class);
    params.add(builder.equal(scheduleRequest.get("createdBy"), usersIdsParam) );
    usersIdsParamList.add(usersIdsParam);
    }

    criteria = criteria.where(params.toArray(new Predicate[0]));

    TypedQuery<ScheduleRequest> query = getJpaTemplate().getEntityManagerFactory().createEntityManager().createQuery(criteria);

    for (int i = 0; i < usersList.size(); i++) {
    query.setParameter(usersIdsParamList.get(i), usersList.get(i).getUsername());
    }

    List<ScheduleRequest> scheduleRequestList = query.getResultList();

The internal Query String is converted to below, so I don't get the records created by the two users, because it is using "AND".

select generatedAlias0 from ScheduleRequest as generatedAlias0 where ( generatedAlias0.createdBy=:param0 ) and ( generatedAlias0.createdBy=:param1 ) order by generatedAlias0.trackingId asc

Please need your advise. Thank you in advance.

Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training Hibernate maven dependency example What is the difference between JPA and Hibernate? How to turn off hbm2ddl? Hibernate hbm2ddl.auto values Hibernate table not mapped error - solution Hibernate error class is not mapped Hibernate Envers custom revinfo table Hibernate Envers - REVINFO table doesn't exist How to enable second level cache in Hibernate 5? How to enable second level cache in Hibernate 5? java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! org.hibernate.validator cannot be resolved error java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion property hibernate.cache.region.factory_class is not given Hibernate example step by step in Eclipse with MySQL How to make Struts Hibernate and Spring Based Login/Registration Application? What is the use of Hibernate Native SQL? What is the use of Hibernate is used? How to integrate Struts and Hibernate in an application? How to create the program in Java for getting the forgotten Password Form? How to write create JPA Many-to-Many Relationship in Java? What is the DAO Layer in any application? What is Spring Struts Hibernate DAO Layer? How to write Example code for CRUD application in hibernate annotation? Where to update the Hibernate tools? Query conversion convert into hql how to convert sql into hql package org.hibernate.util does not exist C3P0 with Hibernate What do you understand by JPA? Hibernate Query Checker Jpa many to many relationship What do you understand by Hibernate in Java? What are criteria Queries in Hibernate? How to use Associations and Joins in Hibernate? Hibernate 4 Example with Eclipse How to learn Hibernate? What is Hibernate Framework? hibernate faq 15/03/14 hibernate 15/03/2014 Hibernate Criteria Wildcard how to add mulitiple projections this is a hibernate question this is a hibernate question this is a hibernate question how to writw connection pool coding Hibernate 4.3 and JPA 2.1 support How to create Hibernate session factory name?

Ads

 
Advertisement null

Ads