简体   繁体   中英

Hibernate “OR” restriction

I have to add Restriction.or to my criteria

I am doing

 Criterion displayName          = Restrictions.ilike("displayName", search, MatchMode.ANYWHERE);
            Criterion lastUpdatedName      = Restrictions.ilike("lu.displayName", search, MatchMode.ANYWHERE);
            Criterion tagTokenVarText      = Restrictions.ilike("tvtext.value", search, MatchMode.ANYWHERE);
            Criterion tagTokenMultiText    = Restrictions.ilike("tmtText.value", search, MatchMode.ANYWHERE);
            Criterion tagTokenSiteVar      = Restrictions.ilike("ttSiteVar.displayName", search, MatchMode.ANYWHERE);
            Criterion tagMultiTokenSiteVar = Restrictions.ilike("tmtSiteVar.displayName", search, MatchMode.ANYWHERE);

        Criterion or1 = Restrictions.or(displayName, lastUpdatedName);
        Criterion or2 = Restrictions.or(tagTokenVarText, tagTokenMultiText);
        Criterion or3 = Restrictions.or(tagTokenSiteVar, tagMultiTokenSiteVar);
        Criterion or4 = Restrictions.or(or1, or2);
        criteria.add(Restrictions.or(or4, or3));

but i do not like this way of creating Criterion or1, or2...

All my variables (displayName....tagMultiTokenSiteVar ) should be OR. Can I add it to the list or something?

Use a Disjunction :

Criteria crit = session.createCriteria(YourClass.class);
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.ilike("lu.displayName", search, MatchMode.ANYWHERE));
// ...add others restrictions...
crit.add(disjunction);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM