af_khan
af_khan

Reputation: 1078

Multiple criteria for same column

I have a column which has same value but other columns differ. I want to write search for this condition. How do i do this ? I am getting duplicate criteria exception.

if(!StringUtils.isEmpty(src)){                           
criteria.createCriteria("params").add(Restrictions.eq("tagName",   
OptionalParams.TagName.SOURCE)).add(Restrictions.ilike("name", "Version")).add(Restrictions.ilike("value", src,MatchMode.START));
}

if(!StringUtils.isEmpty(tgt)){
                criteria.createCriteria("params").add(Restrictions.eq("tagName", OptionalParams.TagName.TARGET))
                .add(Restrictions.ilike("name", "Version")).add(Restrictions.ilike("value", tgt,MatchMode.START));
            }

  57273 Version 0       1.2.0   9dbb
  57275 Version 1       1.2.3   9dbb

Any help is appreciated.

Upvotes: 0

Views: 1139

Answers (1)

PVR
PVR

Reputation: 2524

This might help you.

 Criteria criteria = session.createCriteria("params");
    if(!StringUtils.isEmpty(src)){                           
    criteria.add(Restrictions.eq("tagName",   
    OptionalParams.TagName.SOURCE)).add(Restrictions.ilike("name", "Version")).add(Restrictions.ilike("value", src,MatchMode.START));
    }

    if(!StringUtils.isEmpty(tgt)){
                    criteria.add(Restrictions.eq("tagName", OptionalParams.TagName.TARGET))
                    .add(Restrictions.ilike("name", "Version")).add(Restrictions.ilike("value", tgt,MatchMode.START));
                }

You were getting the exception because, you were writing criteria twice in a same transaction on same object. Also, createCriteria(*.class) should have parameter as class.

For more details please have a look at the http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

Upvotes: 1

Related Questions