Hibernate criterBuilder and or 多个查询

 

//判断处罚类型和金额
predicates.add(
builder.or(
builder.and(
builder.equal(root.get("targetType"), TargetType.TARGET_TYPE_1.getType()),
builder.greaterThanOrEqualTo(root.get("discretionTotal"), BigDecimal.valueOf(10000)))
,
builder.and(
builder.equal(root.get("targetType"), TargetType.TARGET_TYPE_0.getType()),
builder.greaterThanOrEqualTo(root.get("discretionTotal"), BigDecimal.valueOf(100000)))
));

 

https://stackoverflow.com/questions/40872949/generating-correct-and-or-condition-with-jpa-criteria-api/54783639#54783639
SELECT DISTINCT ***
FROM v3_flow_case v3flowcase0_
LEFT OUTER JOIN v3_flow_approval approval1_ ON v3flowcase0_.case_id=approval1_.case_id
WHERE (v3flowcase0_.status = 1)
  AND approval1_.status=1
  AND (v3flowcase0_.punish_type=10
       OR approval1_.approval_type=1)
  AND (v3flowcase0_.target_type=1
       AND v3flowcase0_.discretion_total>=10000
       OR v3flowcase0_.target_type=0
       AND v3flowcase0_.discretion_total>=100000)
  AND v3flowcase0_.case_id=0
  AND v3flowcase0_.status=1
ORDER BY v3flowcase0_.create_time DESC
LIMIT ?
//结果本希望是
AND ((v3flowcase0_.target_type=1
       AND v3flowcase0_.discretion_total>=10000
       )OR (v3flowcase0_.target_type=0
       AND v3flowcase0_.discretion_total>=100000))
//找了半天解决方法,发现不用加内部括号 原因是and优先级比or高
posted @ 2021-10-26 14:43  TIFOSI_Z  阅读(247)  评论(0)    收藏  举报