java - Hibernate criteria with variable amount of OR LIKE clauses -


i new hibernate, , @ moment struggeling firing query use of criteria.

what want achieve is, selecting records contain string in either first or lastname columns.

here have. should give better idea.

session session = hibernateutil.opensession();     criteria criteria = session.createcriteria(contact.class);      if(keywords != null) {         for(string keyword : keywords) {             if(keyword != null && keyword.length() > 0) {                 criteria.add(restrictions.like("firstname", keyword, matchmode.anywhere));                 criteria.add(restrictions.or(restrictions.like("lastname", keyword, matchmode.anywhere), null));             }         }     }      searchmatches = criteria.list(); 

so in end query should along lines of:

select * contacts (firstname '%oe% or lastname '%oe%'), , if multiple keywords given, like:

select * contacts (firstname '%oe% or lastname '%oe%') or (firstname '%roo% or lastname '%roo%')

this select record containing "oe" inside either first or lastname columns. how ever crash code when criteria.list(); run.

here stack trace. can not figure out doing wrong.

caused by: java.lang.nullpointerexception @ org.hibernate.criterion.logicalexpression.tosqlstring(logicalexpression.java:60) @ org.hibernate.loader.criteria.criteriaquerytranslator.getwherecondition(criteriaquerytranslator.java:419) @ org.hibernate.loader.criteria.criteriajoinwalker.<init>(criteriajoinwalker.java:123) @ org.hibernate.loader.criteria.criteriajoinwalker.<init>(criteriajoinwalker.java:92) @ org.hibernate.loader.criteria.criterialoader.<init>(criterialoader.java:93) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1464) @ org.hibernate.internal.criteriaimpl.list(criteriaimpl.java:374) @ com.joey.wishlist.dao.impl.contactdaoimpl.searchcontacts(contactdaoimpl.java:34) @ com.joey.wishlist.services.impl.contactserviceimpl.searchcontacts(contactserviceimpl.java:22) @ com.joey.wishlist.presenters.contactsearchpresenter.search(contactsearchpresenter.java:75) 

edit: 1 step closer.

i fixed error. error caused malfunctioning or.

the code looks this:

        if(keywords != null) {         for(string keyword : keywords) {             if(keyword != null && keyword.length() > 0) {                 criteria.add(                         restrictions.or(                             restrictions.like("lastname", keyword, matchmode.anywhere),                              restrictions.like("firstname", keyword, matchmode.anywhere)                         ));             }         }     } 

which produces query, want. want "and" "or". so:

hibernate:  /* criteria query */ select     this_.id id0_0_,     this_.firstname firstname0_0_,     this_.lastname lastname0_0_      contactmanager this_      (         this_.lastname ?          or this_.firstname ?     )      , (         this_.lastname ?          or this_.firstname ?     ) 

i want be:

hibernate:  /* criteria query */ select     this_.id id0_0_,     this_.firstname firstname0_0_,     this_.lastname lastname0_0_      contactmanager this_      (         this_.lastname ?          or this_.firstname ?     )      or ( //notice difference here         this_.lastname ?          or this_.firstname ?     ) 

every time call criteria.add, adds and statement. want to create disjunction:

disjunction matchdisjunction = restrictions.disjunction() for(string keyword : keywords) {             if(keyword != null && keyword.length() > 0) {                 matchdisjunction.add(                         restrictions.or(                             restrictions.like("lastname", keyword, matchmode.anywhere),                              restrictions.like("firstname", keyword, matchmode.anywhere)                         ));             }         } criteria.add(matchdisjunction); 

or simply:

disjunction matchdisjunction = restrictions.disjunction() for(string keyword : keywords) {             if(keyword != null && keyword.length() > 0) {                 matchdisjunction.add( restrictions.like("lastname", keyword, matchmode.anywhere) );                 matchdisjunction.add( restrictions.like("firstname", keyword, matchmode.anywhere) );             }         } criteria.add(matchdisjunction); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -