hibernate: 用Disjunction和Conjunction构造复杂的查询条件

Disjunction 与 Conjunction 表示逻辑或与逻辑与

可以构造复杂的Sql 语句

Disjunction disjunction = Restrictions.disjunction();
 Criterion cirterion = Restrictions.sqlRestriction("SIMULPORTCAPACITY<SIMULPORTCAPACITYOCUPIED".toLowerCase());
  disjunction.add(cirterion);
cirterion = Restrictions.sqlRestriction("ADSLPORTCAPACITY<ADSLPORTCAPACITYOCCUPIED".toLowerCase());
  disjunction.add(cirterion);
  cirterion = Restrictions.sqlRestriction("LANPORTCAPACITY<LANPORTCAPACITYOCCUPIED".toLowerCase());
  disjunction.add(cirterion);

  Conjunction conjunction = Restrictions.conjunction();
  cirterion = Restrictions.eq("lanportcapacity", 0);
  conjunction.add(cirterion);
  cirterion = Restrictions.eq("simulportcapacity", 0);
  conjunction.add(cirterion);
  cirterion = Restrictions.eq("adslportcapacity", 0);
  conjunction.add(cirterion);

disjunction.add(conjunction);
  queryCriteria.add(disjunction);



构造出的条件如下:
select *
  from aaaa this_
 where (simulportcapacity < simulportcapacityocupied or
       adslportcapacity < adslportcapacityoccupied or
       lanportcapacity < lanportcapacityoccupied or
       (this_.LANPORTCAPACITY = ? and this_.SIMULPORTCAPACITY = ? and
       this_.ADSLPORTCAPACITY = ?))

// 如果 是 :

queryCriteria.add(disjunction);

queryCriteria.add(conjunction);

//那么 sql  :
select *
  from aaaa this_
 where (simulportcapacity < simulportcapacityocupied or
       adslportcapacity < adslportcapacityoccupied or
       lanportcapacity < lanportcapacityoccupied )and
       (this_.LANPORTCAPACITY = ? and this_.SIMULPORTCAPACITY = ? and
       this_.ADSLPORTCAPACITY = ?)





你可能感兴趣的:(Hibernate)