下面这两个CriteriaBuilder方法是如何使用的?您是否曾经使用过它们或看到它们在现实世界的应用程序中使用过?我在网上找不到示例。阅读代码注释:values 和 keys 创建一个集合表达式,可以传递给 size()、isMember() 等。
//get the values and keys collections of the Map, which may then
//be passed to size(), isMember(), isEmpty(), etc
/**
* Create an expression that returns the values of a map.
*
* @param map map
*
* @return collection expression
*/
<V, M extends Map<?, V>> Expression<Collection<V>> values(M map);
/**
* Create an expression that returns the keys of a map.
*
* @param map map
*
* @return set expression
*/
<K, M extends Map<K, ?>> Expression<Set<K>> keys(M map);
但是为什么需要 JPA 来确定集合的大小,或者测试元素是否是集合的成员?看来这可以用 Java 完成,不需要 JPA。
最佳答案
我只回答第二个问题。
Why would JPA be needed to determine the size of a collection, or to test whether an element is a member of a collection? It seems that this can be done in Java, no need for JPA.
这些 JPA 方法用于构建查询。在从数据库中检索数据之前创建条件比加载所有数据然后在 Java 中过滤它更有效。
例如用户有更多的角色。我们想要加载具有特定角色的用户。对于此成员
,使用了 JPQL 保留字。
select u from User u where :specificRole member of u.roles
同理size()
,为空
即可。
https://www.objectdb.com/java/jpa/query/jpql/collection
关于java - JPA CriteriaBuilder 方法 : keys and values. 如何使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55436908/