DetachedCriteria多条件查询or,disjunction

使用Disjunction来做sql中or的配置。

首先看看Disjunction的结构:

public class Disjunction extends Junction {

	protected Disjunction() {
		super("or");
	}

}

public class Junction implements Criterion {
...
}

应用:

String[] diffFlagArray = finder.getDiffFlags().split(",");
			Disjunction disjunction = Restrictions.disjunction();  
			for(String flag : diffFlagArray){
				if(flag.equalsIgnoreCase("portName")){
					disjunction.add(Restrictions.eq("portNameDiff", '1'));
				}else if(flag.equalsIgnoreCase("trustor")){
					disjunction.add(Restrictions.eq("trustorDiff", '1'));
				}else if(flag.equalsIgnoreCase("trustee")){
					disjunction.add(Restrictions.eq("trusteeDiff", '1'));
				}else if(flag.equalsIgnoreCase("custodian")){
					disjunction.add(Restrictions.eq("custodianDiff", '1'));
				}else if(flag.equalsIgnoreCase("investManager")){
					disjunction.add(Restrictions.eq("investManagerDiff", '1'));
				}else if(flag.equalsIgnoreCase("acountManager")){
					disjunction.add(Restrictions.eq("acountManagerDiff", '1'));
				}
			}
			criteria.add(disjunction);

这样生成的sql语句:

and (
                this_.c_port_name_diff=? 
                or this_.c_trustor_diff=? 
                or this_.c_trustee_diff=? 
                or this_.c_custodian_diff=? 
                or this_.c_invest_manager_diff=? 
                or this_.c_acount_manager_diff=?
            ) 


你可能感兴趣的:(java,Hibernate)