我有以下两个实体:
@Entity
class Relation{
@ManyToOne
private User user;
//some other fields
...
}
用户实体有其他实体的集合:
@Entity
class User {
@OneToMany(mappedBy="user")
private Collection<Address> addresses = new ArrayList<Address>();
}
//some other fields
}
是否可以在关系实体中编写一个命名查询,为我提供拥有超过 2 个地址的用户...? 喜欢:
@NamedQuery("从成员资格 m 中选择 m,其中 m.otherfield = ?1 AND m.user.addresses > 2")
换句话说,我如何通过命名查询获取该实体的大小?
谢谢
最佳答案
在 JPQL 查询中,SIZE 函数可用于获取集合的大小。它将集合的路径作为参数并返回元素的数量。
关于java - 用于获取集合大小的 JPA 命名查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930586/