JPA - Get all rows that matches multiple strings

后端 未结 1 1734
别那么骄傲
别那么骄傲 2021-01-24 13:58

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 t

相关标签:
1条回答
  • 2021-01-24 14:28

    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)]

    0 讨论(0)
提交回复
热议问题