Java JPA标准:与集合中的任何id匹配的生成规范
我试图通过JPA CriteriaBuilder构建一个查询,以在给定值的列表中找到所有具有id的对象 这就是我所创造的:Java JPA标准:与集合中的任何id匹配的生成规范,java,jpa,Java,Jpa,我试图通过JPA CriteriaBuilder构建一个查询,以在给定值的列表中找到所有具有id的对象 这就是我所创造的: protected <T> Specification<T> equalsToAnyId(Set<Long> ids) { return (root, query, builder) -> builder.or(root.getModel().getDeclaredSingularAttributes().strea
protected <T> Specification<T> equalsToAnyId(Set<Long> ids) {
return (root, query, builder) -> builder.or(root.getModel().getDeclaredSingularAttributes().stream()
.filter(a -> Collections.singletonList("id").contains(a.getName()))
.map(a -> builder.in(
root.get(a.getName())).value(ids))
.toArray(Predicate[]::new)
);
}
受保护的规范等同于任何ID(设置ID){
return(root,query,builder)->builder.or(root.getModel().getDeclaredSingularAttributes().stream())
.filter(a->Collections.singletonList(“id”).contains(a.getName())
.map(a->builder.in(
root.get(a.getName()).value(id))
.toArray(谓词[]::新)
);
}
这似乎与任何记录(空结果集)都不匹配。有什么问题吗?这似乎是一种非常复杂的编写简单IN-filter的方法。除非您正在编写某种DAO框架,否则当整个查询只需要几行代码时,为什么要接受
root、builder、query
并返回Specification
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(entityClass);
Root<T> root = cq.from(entityClass);
cq.where(root.get("id").in(guids));
return getEntityManager().createQuery(cq).getResultList();
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(entityClass);
Root=cq.from(entityClass);
cq.where(root.get(“id”).in(guids));
返回getEntityManager().createQuery(cq.getResultList();
如果要保留方法签名,请执行以下操作:
protected <T> Specification<T> equalsToAnyId(Set<Long> ids) {
return (root, query, builder) -> builder.or(builder.in(root.get("id")).value(ids));
}
受保护的规范等同于任何ID(设置ID){
return(root、query、builder)->builder.or(builder.in(root.get(“id”)).value(id));
}
尽管您没有指定规范是什么(JPA规范中没有此类类)
通常,如果您不确定查询为什么没有返回预期的数据,请通过persistence.xml
中的特定于提供程序的属性(例如用于hibernate的hibernate.show_SQL
)打开SQL日志记录,或者通过启用持久性提供程序的调试/精细日志记录,例如org.hibernate.SQL
顺便说一句:是的,我们都很高兴java终于有了lambda,但这并不意味着我们必须lambda所有的东西:)特别是你如何流式处理/过滤/映射元模型来获取root.get(“id”)
。。。我觉得很奇怪。这似乎是一种非常复杂的方式来编写一个简单的IN-filter。除非您正在编写某种DAO框架,否则当整个查询只需要几行代码时,为什么要接受root、builder、query
并返回Specification
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(entityClass);
Root<T> root = cq.from(entityClass);
cq.where(root.get("id").in(guids));
return getEntityManager().createQuery(cq).getResultList();
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(entityClass);
Root=cq.from(entityClass);
cq.where(root.get(“id”).in(guids));
返回getEntityManager().createQuery(cq.getResultList();
如果要保留方法签名,请执行以下操作:
protected <T> Specification<T> equalsToAnyId(Set<Long> ids) {
return (root, query, builder) -> builder.or(builder.in(root.get("id")).value(ids));
}
受保护的规范等同于任何ID(设置ID){
return(root、query、builder)->builder.or(builder.in(root.get(“id”)).value(id));
}
尽管您没有指定规范是什么(JPA规范中没有此类类)
通常,如果您不确定查询为什么没有返回预期的数据,请通过persistence.xml
中的特定于提供程序的属性(例如用于hibernate的hibernate.show_SQL
)打开SQL日志记录,或者通过启用持久性提供程序的调试/精细日志记录,例如org.hibernate.SQL
顺便说一句:是的,我们都很高兴java终于有了lambda,但这并不意味着我们必须lambda所有的东西:)特别是你如何流式处理/过滤/映射元模型来获取root.get(“id”)
。。。我觉得很奇怪。是否愿意分享JPQL等同于什么,执行什么样的SQL?是否愿意分享JPQL等同于什么,执行什么样的SQL?