本文介绍了JPA-获取与多个字符串匹配的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表UserData,其中包含名称,userRole,电话号码等字段.UserRole字段可以具有ADMIN,USER,MANAGER,OWNER,VIEWER等值.我试图编写一个JPA本机查询,以检索userRole为ADMIN,USER,MANAGER的所有行.

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....

从屏幕上选择搜索条件.什么是JPA查询,类似于下面的SQL查询.

The search criteria is selected from the screen. What can be the JPA query which is similar to the below SQL query.

从UserData中选择*,其中userRole = ADMIN或userRole = USER OR userRole = MANAGER,依此类推....

SELECT * from UserData where userRole = ADMIN OR userRole = USER OR userRole = MANAGER and so on....

预先感谢您的帮助.

推荐答案

您可以尝试一下.

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();

对于休眠状态

Query query = session.createQuery("SELECT u FROM UserData u WHERE u.userRole IN (:roles)");
query.setParameterList("roles", roles);

[特定于实现/版本-:namedParameter OR(:namedParameter)]

这篇关于JPA-获取与多个字符串匹配的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:26