java - 用于获取集合大小的 JPA 命名查询

标签 java jpa named

我有以下两个实体:

@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/

相关文章:

java - 没有外键的 JPA OneToOne 关联总是失败

c++ - boost 正则表达式中的命名捕获/组列表

java - 获取与特定组或上下文相关的 Json 属性

java - Java VM 是否跳过零乘法?

hibernate - JPA 2 条件查询在 select 语句中的 FK 问题的表达式中,其中 n 是 #values

regex - Notepad++正则表达式替换命名组

r - 使用 purrr 的 map 函数的输入创建一个命名列表作为 R 中的输出

java:全局变量在线程中是否可见

java - header 签名无效;在 excel 文档上使用 Apache POI 的 IOException

java - 有什么方法可以在 JPA 或 Spring-data 中设置默认外键吗?