Reputation: 79
I have a table UserData with fields such as name, userRole, phonenumber etc. UserRole field can have values like ADMIN, USER, MANAGER, OWNER, VIEWER and many more. I am trying to write a JPA native query that retrieves all the rows where the userRole is ADMIN, USER, MANAGER....
The search criteria is selected from the screen. What can be the JPA query which is similar to the below SQL query.
SELECT * from UserData where userRole = ADMIN OR userRole = USER OR userRole = MANAGER and so on....
Thanks in advance for your help.
Upvotes: 1
Views: 1653
Reputation: 11612
You can try this.
String selectQuery= "SELECT u FROM UserData u WHERE u.userRole IN :roles";
Query query = em.createQuery(selectQuery, UserData.class);
List<String> roles = Arrays.asList("ADMIN", "USER", "MANAGER");
query.setParameter("roles", roles);
List<UserData> users = query.getResultList();
Edit: For Hibernate
Query query = session.createQuery("SELECT u FROM UserData u WHERE u.userRole IN (:roles)");
query.setParameterList("roles", roles);
[It's implementation/version specific - :namedParameter OR (:namedParameter)]
Upvotes: 4