使用Spring Data Jpa的CriteriaQuery时,如何避免陷入查询陷阱?

2026-04-30 05:202阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计962个文字,预计阅读时间需要4分钟。

使用Spring Data Jpa的CriteriaQuery时,如何避免陷入查询陷阱?

使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,如果条件为空,可能会导致查询不到任何结果,并且不是期望的返回所有结果。这是因为CriteriaQuery在构建查询时,会将每个条件视为必须满足的约束。如果条件为空,则相当于没有添加任何约束,导致查询结果为空。

例如,以下代码展示了当predicates为空时的情况:

javaCriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();CriteriaQuery query=criteriaBuilder.createQuery(YourEntity.class);Root root=query.from(YourEntity.class);

// 假设predicates为空List predicates=new ArrayList();

query.select(root).where(predicates.toArray(new Predicate[0]));

List result=entityManager.createQuery(query).getResultList();

在这种情况下,由于predicates为空,查询实际上变成了一个没有任何过滤条件的全表查询,但因为没有添加任何实际的过滤条件,所以返回的结果为空。

为了避免这种情况,确保在构建查询时,即使没有具体的过滤条件,也至少添加一个恒为真的条件,例如`criteriaBuilder.conjunction()`,以确保查询能够返回所有数据:

javaList predicates=new ArrayList();predicates.add(criteriaBuilder.conjunction()); // 添加一个恒为真的条件

query.select(root).where(predicates.toArray(new Predicate[0]));

这样,即使没有具体的过滤条件,查询也会返回所有数据。

使用Spring Data Jpa的CriteriaQuery时,如何避免陷入查询陷阱?

使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,可能会遇到一个陷阱,当条件为空时,查询不到任何结果,并不是期望的返回所有结果。这是为什么呢?

例如下述代码,当predicates为空时,返回结果总是为空。

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... return cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0])); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

看下or的注释就明白了,因为空条件总是为false,而and的空条件总是为true。所以,如果最后是and就没有问题,只有or的时候有问题。

public interface CriteriaBuilder { /** * Create a conjunction of the given restriction predicates. * A conjunction of zero predicates is true. * @param restrictions zero or more restriction predicates * @return and predicate */ Predicate and(Predicate... restrictions); /** * Create a disjunction of the given restriction predicates. * A disjunction of zero predicates is false. * @param restrictions zero or more restriction predicates * @return or predicate */ Predicate or(Predicate... restrictions); }

所以正确的写法应该这样:

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... return predicates.isEmpty() ? cb.conjunction() : cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0])); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

如果条件为空则返回一个空conjunction,也就是空的and,总是为true。

公司项目的代码中常见这种写法:

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... if (predicates.isEmpty()) { cq.where(); } else { cq.where(cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0]))); } return cq.getRestriction(); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

也能正常工作,但是其实没有必要在toPredicate方法中调用where,toPredicate只需要返回条件,外层会调用where。

public interface Specification<T> extends Serializable { /** * Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given * {@link Root} and {@link CriteriaQuery}. * * @param root must not be {@literal null}. * @param query must not be {@literal null}. * @param criteriaBuilder must not be {@literal null}. * @return a {@link Predicate}, may be {@literal null}. */ @Nullable Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder); }

本文作者: 钟潘
本文链接: zhongpan.tech/2020/07/20/035-a-trap-for-using-criteriaquery/

以上就是CriteriaQuery使用的一个陷阱的详细内容,更多关于CriteriaQuery 陷阱的资料请关注易盾网络其它相关文章!

标签:CriteriaQ

本文共计962个文字,预计阅读时间需要4分钟。

使用Spring Data Jpa的CriteriaQuery时,如何避免陷入查询陷阱?

使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,如果条件为空,可能会导致查询不到任何结果,并且不是期望的返回所有结果。这是因为CriteriaQuery在构建查询时,会将每个条件视为必须满足的约束。如果条件为空,则相当于没有添加任何约束,导致查询结果为空。

例如,以下代码展示了当predicates为空时的情况:

javaCriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();CriteriaQuery query=criteriaBuilder.createQuery(YourEntity.class);Root root=query.from(YourEntity.class);

// 假设predicates为空List predicates=new ArrayList();

query.select(root).where(predicates.toArray(new Predicate[0]));

List result=entityManager.createQuery(query).getResultList();

在这种情况下,由于predicates为空,查询实际上变成了一个没有任何过滤条件的全表查询,但因为没有添加任何实际的过滤条件,所以返回的结果为空。

为了避免这种情况,确保在构建查询时,即使没有具体的过滤条件,也至少添加一个恒为真的条件,例如`criteriaBuilder.conjunction()`,以确保查询能够返回所有数据:

javaList predicates=new ArrayList();predicates.add(criteriaBuilder.conjunction()); // 添加一个恒为真的条件

query.select(root).where(predicates.toArray(new Predicate[0]));

这样,即使没有具体的过滤条件,查询也会返回所有数据。

使用Spring Data Jpa的CriteriaQuery时,如何避免陷入查询陷阱?

使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,可能会遇到一个陷阱,当条件为空时,查询不到任何结果,并不是期望的返回所有结果。这是为什么呢?

例如下述代码,当predicates为空时,返回结果总是为空。

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... return cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0])); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

看下or的注释就明白了,因为空条件总是为false,而and的空条件总是为true。所以,如果最后是and就没有问题,只有or的时候有问题。

public interface CriteriaBuilder { /** * Create a conjunction of the given restriction predicates. * A conjunction of zero predicates is true. * @param restrictions zero or more restriction predicates * @return and predicate */ Predicate and(Predicate... restrictions); /** * Create a disjunction of the given restriction predicates. * A disjunction of zero predicates is false. * @param restrictions zero or more restriction predicates * @return or predicate */ Predicate or(Predicate... restrictions); }

所以正确的写法应该这样:

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... return predicates.isEmpty() ? cb.conjunction() : cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0])); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

如果条件为空则返回一个空conjunction,也就是空的and,总是为true。

公司项目的代码中常见这种写法:

public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) { Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> { root.join("user", JoinType.LEFT); root.join("tenant", JoinType.LEFT); List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>(); ...... if (predicates.isEmpty()) { cq.where(); } else { cq.where(cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0]))); } return cq.getRestriction(); }; PageRequest pagable = PageRequest.of(0, 5); Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable); return page; }

也能正常工作,但是其实没有必要在toPredicate方法中调用where,toPredicate只需要返回条件,外层会调用where。

public interface Specification<T> extends Serializable { /** * Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given * {@link Root} and {@link CriteriaQuery}. * * @param root must not be {@literal null}. * @param query must not be {@literal null}. * @param criteriaBuilder must not be {@literal null}. * @return a {@link Predicate}, may be {@literal null}. */ @Nullable Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder); }

本文作者: 钟潘
本文链接: zhongpan.tech/2020/07/20/035-a-trap-for-using-criteriaquery/

以上就是CriteriaQuery使用的一个陷阱的详细内容,更多关于CriteriaQuery 陷阱的资料请关注易盾网络其它相关文章!

标签:CriteriaQ