Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How to add restrictions dinamically to criteriaBuilder

The key to creating a dynamic restriction is to build the restriction first then add it as a whole to the CriteriaQuery. Your code should be changed to look as follows.

Predicate restriction = qb.conjunction(); // assume starting with True.
if (a) {
  restriction = qb.and(restriction, qb.equal(X, Y));
}
if (b) {
  restriction = qb.and(restriction, qb.equal(Z, C));
}

criteriaQuery.where(restriction);

--Gordon

Diego Coronel wrote:
Hi,

 Im trying to dinamic add restrictions to criteria, but i dont know how to
do it.. all samples in jpa-reference just always add it on
criteriaQuery.where(xxxx,yyy,zzz);

 Can you tell me how to do something like:

 if ( a ) {
  criteriaQuery.addRestriction( qb.equal( X, Y ) );
}

 if ( b ) {
  criteriaQuery.addRestriction( qb.equal( Z, C ) );
}

i wanna to construct my query restrictions in runtime. just adding it when
necessary.

... sry about english.


Back to the top