java - JPA criteriabuilder where statement -
i achieve following statement in criteriaquery.
(event.time >= now() or (event.time >= '$clubhours' && (`eventselecttype`.`type`='cafe' || `eventselecttype`.`type`='dj')) or (event.endtime >= now() && (`eventselecttype`.`type`='festival' or `eventselecttype`.`type`='dj' or `eventselecttype`.`type`='cafe')))
i tried following ;
eqr.where( cb.or( cb.or( cb.greaterthanorequalto(eventroot.<date>get("time"), curdate) ), cb.or( cb.and( cb.greaterthanorequalto(eventroot.<date>get("time"), cal.gettime()) ), cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj") ) ), cb.or( cb.and( cb.greaterthanorequalto(eventroot.<date>get("endtime"), curdate) ), cb.or( cb.equal(eventtype.<string>get("type"), "festival"), cb.equal(eventtype.<string>get("type"), "dj"), cb.equal(eventtype.<string>get("type"), "cafe") ) ) ), cb.equal(eventroot.<integer>get("id"), eventid), cb.equal(eventroot.<integer>get("active"), 1) );
but generates 1 big or statement
(event0_.time>=? or event0_.time>=? or eventselec13_.type=? or eventselec13_.type=? or event0_.endtime>=? or eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?)
how result i'm looking for?
edit
i have this
predicate eventtypeiscafeordj = cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj") ); predicate eventtypeisfestivaldjorcafe = cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj"), cb.equal(eventtype.<string>get("type"), "festival") ); predicate timegreaterorequalto = cb.greaterthanorequalto(eventroot.<date>get("time"), cal.gettime()); predicate endtimegreaterorequalto = cb.greaterthanorequalto(eventroot.<date>get("endtime"), curdate); predicate equaltoid = cb.equal(eventroot.<integer>get("id"), eventid); eqr.where(timegreaterorequalto, cb.or( cb.and( timegreaterorequalto, eventtypeiscafeordj ), cb.and(endtimegreaterorequalto, eventtypeisfestivaldjorcafe )), equaltoid);
which outputs
where event0_.time>=? , (event0_.time>=? , (eventselec13_.type=? or eventselec13_.type=?) or event0_.endtime>=? , (eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?)) , event0_.id=633188
but has be
where (event0_.time>=? or (event0_.time>=? , (eventselec13_.type=? or eventselec13_.type=?)) or (event0_.endtime>=? , (eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?))) , event0_.id=633188
it still isn't want , cant seem work way want to.
start obvious, first problem:
event.time >= '$clubhours' && (`eventselecttype`.`type`='cafe' || `eventselecttype`.`type`='dj'))
this should constructed
cb.and( cb.greaterthanorequalto(eventroot.<date>get("time"), cal.gettime()), cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj") ) ), ...
instead have:
cb.and( cb.greaterthanorequalto(eventroot.<date>get("time"), cal.gettime()) ), cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj") )
so, basically, need sure @ parentheses , not indenting in order understand code doing. side note, better off constructing predicate in steps code easier read , understand. e.g.:
predicate eventtypeiscafeordj = cb.or( cb.equal(eventtype.<string>get("type"), "cafe"), cb.equal(eventtype.<string>get("type"), "dj") ); predicate timegreaterorequalto = cb.greaterthanorequalto(eventroot.<date>get("time"), cal.gettime()); eqr.where( cb.and( timegreaterorequalto, eventtypeiscafeordj ) );
the database slower part of code , optimizer make same runtime out or either set of code, should make easier on , others. further, time tested way of debugging coding problem, when see parens.
Comments
Post a Comment