Hibernate CriteriaBuiler .or()
Hibernate CriteriaBuiler .or()
我已经为我的查询构造了以下或谓词。我想检索所有没有关联客户的订单,以及有关联且具有几种特定状态之一的订单。
Predicate p1 = cbuilder.isNull(root.get(Order_.customer));
Predicate p2 = root.get(Order_.customer).get(Customer_.status).in(statusList);
predicates.add(cbuilder.or(p2, p1));
...
cquery.where(cbuilder.and(predicates.toArray(new Predicate[predicates.size()])));
对于示例数据集,p1 和 p2 都为真。但是,查询 returns 没有结果。换句话说,这个:
Predicate p1 = cbuilder.isNull(root.get(Order_.customer));
predicates.add(p1);
还有这个:
Predicate p2 = root.get(Order_.customer).get(Customer_.status).in(statusList);
predicates.add(p2);
两个 return 结果,但是当我尝试添加 .or() 运算符时,它 return 没有结果。以下为部分
生成的 sql 查询,我在这里做错了什么?
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_,
...
from
Order order0_
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?)
or
order0_.customer_customerId is null)
通过仅将 p1 添加到谓词生成了 sql 查询:
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_
...
from
Order order0_
where
(order0_.customer_customerId is null)
通过仅将 p2 添加到谓词生成了 sql 查询:
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_,
from
Order order0_
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?))
当Customer 为null 时,此部分返回false,因为无法满足第一个谓词(where ..)。首先添加一个子查询并从查询中取出第二个谓词,修复它。
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?)
or
order0_.customer_customerId is null)
我已经为我的查询构造了以下或谓词。我想检索所有没有关联客户的订单,以及有关联且具有几种特定状态之一的订单。
Predicate p1 = cbuilder.isNull(root.get(Order_.customer));
Predicate p2 = root.get(Order_.customer).get(Customer_.status).in(statusList);
predicates.add(cbuilder.or(p2, p1));
...
cquery.where(cbuilder.and(predicates.toArray(new Predicate[predicates.size()])));
对于示例数据集,p1 和 p2 都为真。但是,查询 returns 没有结果。换句话说,这个:
Predicate p1 = cbuilder.isNull(root.get(Order_.customer));
predicates.add(p1);
还有这个:
Predicate p2 = root.get(Order_.customer).get(Customer_.status).in(statusList);
predicates.add(p2);
两个 return 结果,但是当我尝试添加 .or() 运算符时,它 return 没有结果。以下为部分 生成的 sql 查询,我在这里做错了什么?
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_,
...
from
Order order0_
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?)
or
order0_.customer_customerId is null)
通过仅将 p1 添加到谓词生成了 sql 查询:
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_
...
from
Order order0_
where
(order0_.customer_customerId is null)
通过仅将 p2 添加到谓词生成了 sql 查询:
休眠:
select
order0_.orderId as orderId1_1_,
order0_.customer_customerId as customer_s10_1_,
from
Order order0_
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?))
当Customer 为null 时,此部分返回false,因为无法满足第一个谓词(where ..)。首先添加一个子查询并从查询中取出第二个谓词,修复它。
cross join Customer customer1_
where
order0_.customer_customerId=customer1_.customerId
and
(customer1_.status in (? , ? , ? , ? , ? , ? , ? , ?)
or
order0_.customer_customerId is null)