user1474111
user1474111

Reputation: 1526

Creating where clause by spring specifications

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

Answers (1)

Oleksii Valuiskyi
Oleksii Valuiskyi

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

Related Questions