Reputation: 1526
I am trying to create a complex criteria in where clause by using Spring Specifications. So I am trying to build this native sql :
SELECT * FROM Student
WHERE grade > 10
AND age is null
OR (
( gender = 1 and age > 20 ) or (gender !=1 and age > 18)
)
But at the end query which is builded omits the outest brackets, so it turns out to be :
SELECT * FROM Student
WHERE grade > 10
AND ( age is null
OR gender = 1
and age > 20
OR gender !=1
and age > 18 )
)
And this is how I combine specs:
Specification<Student> spec = new GradeSpec()
.and(new AgeNotNullSpec())
.or(new Gender1Spec().or(new Gender2Spec()))
What am I doing wrong or what should I do to group few criteria together inside the where clause ?
Upvotes: 2
Views: 565
Reputation: 2851
Try to change the statement this way
Specification<Student> spec = (new GradeSpec().and(new AgeNotNullSpec()))
.or(new Gender1Spec().or(new Gender2Spec()));
Upvotes: 0