Adding org.hibernate.criterion.Restriction to GORM createCriteria generates valid, but wrong SQL, delivering false data
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
### Steps to Reproduce
grails create-app brokenMixingOfHibernateCriterionWithFormCriteria
cd brokenMixingOfHibernateCriterionWithFormCriteria
./grailsw create-domain-class Author
./grailsw create-domain-class Book
```
class Book {
String bookName
Author author
static constraints = {
}
}
```
```
class Author {
String authorName
static constraints = {
}
}
```
Have this clause in logback.xml:
```
```
Try to use the domain classes in a controller or service as follows:
Use createCriteria and add a org.hibernate.criterion.Restriction to the query in a or-clause.
```
final authors = Author.createCriteria().listDistinct {
HibernateCriteriaBuilder hibernateCriteriaBuilder = delegate as HibernateCriteriaBuilder
hibernateCriteriaBuilder.projections {
hibernateCriteriaBuilder.property "id"
}
or {
add Restrictions.eq("id", 1L)
eq "authorName", "author1"
}
}
```
Execute the code.
### Expected Behaviour
Expected generated sql (to be seen on console):
```
SELECT this_.id AS y0_
FROM author this_
WHERE this_.id = ?
OR ( this_.author_name = ? )
```
### Actual Behaviour
generated sql (to be seen on console):
```
SELECT this_.id AS y0_
FROM author this_
WHERE this_.id = ?
AND ( this_.author_name = ? )
```
Note, that there is an AND clause generated instead of an OR clause.
### Environment Information
- **Operating System**: Ubuntu 20.04 LTS
- **Grails Version (if using Grails):** Grails 5.1.2
- **JDK Version:** Java 11.0.13
### Demo
https://github.com/scaiandre/grails-gorm-issue-detachedCriteria
./gradlew bootRun
curl http://localhost:8080/brokenMixingOfHibernateCriterionWithFormCriteria/
Contributor guide
Research direction
Start with the createCriteria entry point in the controller or service and the HibernateCriteriaBuilder usage shown in the report; use the demo repository and enable SQL logging in logback.xml. Run ./gradlew bootRun and the supplied curl request, then confirm that mixing Restrictions.eq with eq inside or generates OR SQL and returns the expected records.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100