开发者

Hibernate criterion that matches against everything in some cases

开发者 https://www.devze.com 2023-02-06 00:13 出处:网络
is it possible to do something like criteria.add(Expression.eq(\"product\", \" * \")) for it to match everything ?

is it possible to do something like

criteria.add(Expression.eq("product", " * "))  

for it to match everything ?

It would be handy in case that you get a variable that can have many values and a special value "all" that matches against everything. So you can call a method like this and if the variable had a value of "all", then the expression would match everything.

public void someFunction(String variable) {

    criteria.add(Expression.eq("product", variable))  

}

EDITED: If I had 2 select fields with Source Language & Destination Language and a table listing entries based on which language combination it contains, with IF STATEMENTS it would look like this: (DynamicQuery is an abstraction for Criteria)

private DynamicQuery addCriteria(DynamicQuery dq, String languageFrom, String languageTo){

    if (null != languageFrom && !languageFro开发者_运维问答m.isEmpty() && null != languageTo && !languageTo.isEmpty()){

        if(languageFrom.equals("All") && languageTo.equals("All")) {
            return dq;
        } else if (!languageFrom.equals("All") && !languageTo.equals("All")) {
            dq.add(PropertyFactoryUtil.forName("languageFrom").eq(languageFrom));
            dq.add(PropertyFactoryUtil.forName("languageTo").eq(languageTo));    
            return dq;
        } else if (languageFrom.equals("All") && !languageTo.equals("All")) {
            dq.add(PropertyFactoryUtil.forName("languageTo").eq(languageTo));   
            return dq;
        } else if (!languageFrom.equals("All") && languageTo.equals("All")) {
            dq.add(PropertyFactoryUtil.forName("languageFrom").eq(languageFrom)); 
            return dq;
        } 
    } 
}

It's disgusting, consider there would be one more field, it would be even more disgusting...One simply must cover all the combinations that might occur ... That's why I would like to have something like REGEX, because I would assign variable the reqex "*" value and I would add two lines like this

dq.add(PropertyFactoryUtil.forName("languageFrom").eq(languageFromVariable));
dq.add(PropertyFactoryUtil.forName("languageTo").eq(languageToVariable));   


if (null != variable && !variable.isEmpty()){
    criteria.add(Expression.eq("product", variable)) ;
}

If you only need it when it is selected. You may wanna add an if statement so criteria is added only when needed.

Edit 1:

You can define a method if you are gonna use it for more than fields. Even you can define different criterias inside here. Easy maintance (as is used by all and centrilized), code reuse (change at one point at it will be effective to all)

addCriteria(Criteria criteria, String name, String value){

 //you wanna handle special cases like null == name as well accordingly

    if (null != value && !value.isEmpty() && null != name && !name.isEmpty()){
        criteria.add(Expression.eq(name, value)) ;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消