简体   繁体   English

JPA命名查询以获取集合的大小

[英]JPA named query to get size of a collection

I have the following two entities: 我有以下两个实体:

@Entity
class Relation{

@ManyToOne
private User user;

//some other fields

...
}

The user entity has a collection of other entities: 用户实体具有其他实体的集合:

@Entity
class User {

@OneToMany(mappedBy="user")
private Collection<Address> addresses = new ArrayList<Address>();
}

//some other fields

}

Is it possible to write a named query in the Relation entity which gives me the users which have more than 2 addresses...? 是否可以在Relation实体中编写一个命名查询,该查询为我提供了两个以上地址的用户...? like: 喜欢:

@NamedQuery("SELECT m from Membership m where m.otherfield = ?1 AND m.user.addresses > 2")

in other words, how can i get the size of this entity with a named query? 换句话说,如何通过命名查询获取此实体的大小?

thx 谢谢

In JPQL query SIZE function can be used to get size of collection. 在JPQL中,查询SIZE函数可用于获取集合的大小。 It takes path to the collection as argument and returns number of elements. 它以集合的路径作为参数并返回元素数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM