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

Hi Gordon, 

 Tks, it worked.. but i got another issue that i dont know how to resolve..

 After query is done i tried entityManager.getCriteriaBuilder() or any other way to clear my restrictions.... with your solution (that works ;) ) im getting this problem: 


FINE: SELECT ID, STATUS, LOGIN, NOME, PASSWORD FROM geral.USUARIO WHERE ((LOGIN = ?) AND (PASSWORD = ?))
bind => [a, a] --> this is what i want

FINE: SELECT ID, STATUS, LOGIN, NOME, PASSWORD FROM geral.USUARIO WHERE ((((LOGIN = ?) AND (PASSWORD = ?)) AND (LOGIN = ?)) AND (PASSWORD = ?))
bind => [a, a, diego, diego] --> it should be like query before... its always adding restrictions..

PS: I want to use same entityManager instance or its better to create one instance for each query ?

Sry. about english and tks again.


2010/1/4 Gordon Yorke <gordon.yorke@xxxxxxxxxx>
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.
 
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top