英文:
SubQuery in a CriteriaQuery
问题
以下是翻译好的代码部分:
我有一个抽象类 **Entity**,以及这两个继承自 Entity 的实体 **Role** 和 **User**:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Entity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id = 0;
}
@Entity
public class Role extends Entity {
@Column(name = "NAME", unique = true, nullable = false)
private String name;
}
@Entity
public class User extends Entity {
@Column(name = "ABBREVIATION", unique = true, nullable = false)
private String abbreviation;
// 用户可以拥有多个角色
@ManyToMany
@JoinTable(
name = "USER_ROLE",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
private Set<Role> roles = new HashSet<>();
}
现在,当我点击一个角色时,我希望显示用户的缩写。用于此的原生查询如下:
query = em.createNativeQuery("SELECT ABBREVIATION FROM dbtest.user WHERE ID IN"
+ " (SELECT USER_ID FROM dbtest.user_role WHERE ROLE_ID = " + role.getId() + ")");
我现在已经创建了一个标准查询,但它总是返回一个空列表。
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Role> cq = cb.createQuery(Role.class);
Root<User> root = cq.from(User.class);
Subquery<Long> subquery = cq.subquery(Long.class);
Root<Role> subfrom = subquery.from(Role.class);
subquery.select(subfrom.get(Role_.id));
subquery.where(cb.equal(subfrom.get(Role_.id), role.getId()));
cq.multiselect(root.get(User_.abbreviation));
cq.where(cb.in(root.get(User_.id)).value(subquery));
query = em.createQuery(cq);
return query.getResultList();
这里有什么问题?有人可以帮忙吗?
英文:
I have the abstract class Entity and these two Entities Role and User which extends from Entity:
@Entity@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Entity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id = 0; }
@Entity
public class Role extends Entity{
@Column(name = "NAME", unique = true, nullable = false)
private String name;
}
@Entity
public class User extends Entity{
@Column(name = "ABBREVIATION", unique = true, nullable = false)
private String abbreviation;
//The user can have several roles
@ManyToMany
@JoinTable(
name = "USER_ROLE",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
private Set<Role> roles = new HashSet<>();
}
Now when I click on a role, I want the abbreviations of the users to be displayed.
The native query for it is:
query = em.createNativeQuery("SELECT ABBREVIATION FROM dbtest.user WHERE ID IN"
+ " (SELECT USER_ID FROM dbtest.user_role WHERE ROLE_ID = " + role.getId() + ")");
i have now created a criteria query of it, which always gives me an empty list.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Role> cq = cb.createQuery(Role.class);
Root<User> root = cq.from(User.class);
Subquery subquery = cq.subquery(Role.class);
Root subfrom = subquery.from(Role.class);
subquery.select(subfrom.get(Role_.id));
subquery.where(cb.equal(subfrom.get(Role_.id), role.getId()));
cq.multiselect(root.get(User_.abbreviation));
cq.where(cb.equal(root.get(User_.id), subquery));
query = em.createQuery(cq);
return query.getResultList();
What is here wrong? Can anyone help ?
答案1
得分: 3
首先,设置正确的类型:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<User> root = cq.from(User.class);
Join<User, Role> joinRole = root.join(User_.roles, JoinType.Inner);
Subquery<Long> subquery = cq.subquery(Long.class);
Root<Role> subfrom = subquery.from(Role.class);
subquery.select(subfrom.get(Role_.id));
subquery.where(cb.equal(subfrom.get(Role_.id), role.getId()));
cq.multiselect(root.get(User_.abbreviation));
cq.where(cb.equal(root.get(User_.id), subquery.getSelection()));
query = em.createQuery(cq);
return query.getResultList();
另外,主查询的条件应为 in 而非 equals,请将这部分代码:
cq.where(cb.equal(root.get(User_.id), subquery.getSelection()));
替换为:
cq.where(joinRole.get(Role_.id).in(subquery.getSelection()));
英文:
First of all, set the correct types:
CriteriaBuilder cb = em.getCriteriaBuilder();
//CriteriaQuery<Role> cq = cb.createQuery(Role.class);
//The type of the data returned in multiselect.
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<User> root = cq.from(User.class);
//Add to compare
Join<User,Role> joinRole = root.join(User_.roles,JoinType.Inner);
//The type of the data returned in select.
//Subquery subquery = cq.subquery(Role.class);
Subquery<Long> subquery = cq.subquery(Long.class);
//Root subfrom = subquery.from(Role.class);
//We set the data type in the Root
Root<Role> subfrom = subquery.from(Role.class);
subquery.select(subfrom.get(Role_.id));
subquery.where(cb.equal(subfrom.get(Role_.id), role.getId()));
cq.multiselect(root.get(User_.abbreviation));
//cq.where(cb.equal(root.get(User_.id), subquery));
//To use in the where subquery, call the getSelection () method
cq.where(cb.equal(root.get(User_.id), subquery.getSelection()));
query = em.createQuery(cq);
return query.getResultList();
Also, the where of the main query is an in not an equals, change this
cq.where(cb.equal(root.get(User_.id), subquery.getSelection()));
by this
cq.where(joinRole.get(Role_.id).in(subquery.getSelection()));
答案2
得分: 1
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Role> cq = cb.createQuery(Role.class);
Root<User> root = cq.from(User.class);
Subquery<Long> subquery = cq.subquery(Long.class);
Root<Role> subqueryRoot = subquery.from(Role.class);
Predicate subqueryPredicate =
cb.equal(subqueryRoot.get(Role_.id), role.getId());
//!!! 我怀疑在这里您需要选择 USER_ID,但是 Role 实体中并不包含这样的字段
subquery.select(subqueryRoot.get(Role_.id))
.where(subqueryPredicate);
Predicate queryPredicate = cb.in(root.get(User_.id)).value(subquery);
cq.multiselect(root.get(User_.abbreviation)).where(queryPredicate);
英文:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Role> cq = cb.createQuery(Role.class);
Root<User> root = cq.from(User.class);
Subquery<Long> subquery = cq.subquery(Long.class);
Root<Role> subqueryRoot = subquery.from(Role.class);
Predicate subqueryPredicate =
cb.equal(subqueryRoot.get(Role_.id), role.getId());
//!!! I suspect here you have to select USER_ID, but Role entity does not contain such field
subquery.select(subqueryRoot.get(Role_.id))
.where(subqueryPredicate);
Predicate queryPredicate = cb.in(root.get(User_.id)).value(subquery);
cq.multiselect(root.get(User_.abbreviation)).where(queryPredicate);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论