我有两个java对象。
SELECT
COUNT(distinct u.uid) AS NOOFROWS
FROM
ABC u
INNER JOIN
(SELECT uemail
FROM ABC
GROUP BY uemail
HAVING COUNT(1) = 1) AS u2 ON u.uemail = u2.uemail
INNER JOIN
PQR on u.uid = PQR.uid
INNER JOIN
XYZ p ON u.uid = p.uid
列)我有一个所有用户索引列表的列表,user_index
作为给定输入,我想基于此usersIndexList
获取所有地址对象。我在另一个帖子上找到了一个例子。并试图遵循它,但它不起作用。
JPA CriteriaBuilder - How to use "IN" comparison operator
我的代码:
usersIndexList
但是这个查询没有返回所需的结果:(
有人可以告诉我,我做错了,或者通过List<String> usersIndexList= new ArrayList<String> ();
for (User u : usersList) {
usersIndexList.add(u.getIndex());
}
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<User> subQuery = criteriaBuilder.createQuery(User.class);
Root<User> fromUser= subQuery.from(User.class);
Expression<String> exp = fromUser.get("user_index");
Predicate predicate = exp.in(usersIndexList);
subQuery.where(predicate);
TypedQuery<User> query = getEntityManager().createQuery(subQuery);
return query.getResultList();
或nativequery
或任何其他方式给我一个替代解决方案
答案 0 :(得分:1)
根据您的问题,您希望根据此usersIndexList
获取所有地址对象。但是在您的代码中,您选择的是User
个对象,而不是Address
。我的理解是正确的吗?如果是,请将您的根目录更改为Address
,如下所示 -
List<String> usersIndexList= new ArrayList<String> ();
for (User u : usersList) {
usersIndexList.add(u.getIndex());
}
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Address> subQuery = criteriaBuilder.createQuery(Address.class);
Root<Address> fromAddress= subQuery.from(Address.class);
Expression<String> exp = fromAddress.get("user_index");
Predicate predicate = exp.in(usersIndexList);
subQuery.where(predicate);
TypedQuery<Address> query = getEntityManager().createQuery(subQuery);
return query.getResultList();