Clausule between on createCriteria().list not working with ID attribute
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
### Expected Behavior
Domain classes:
```
abstract class Contract implements Signable, Fileable, IdempotentResponse {
Long loanId
Integer installmentsNumber
}
```
```
class Ccb extends Contract {
Long number
}
```
Service:
```
@Transactional
class RegenerateCcbJobService {
execute(String idFrom, String idTo) {
ConcurrentLinkedQueue ccbIds = new ConcurrentLinkedQueue(
Ccb.createCriteria().list {
between("id", Long.valueOf(idFrom), Long.valueOf(idTo))
projections {
id()
}
}
)
for(long i in ccbIds) {
generateCcb(i);
}
}
private generateCcb(long ccbId) {
try {
Ccb ccb = (Ccb) Ccb.createCriteria().get {
eq("id", ccbId)
}
ccbFilerService.generateBaseDocument(ccb)
}
}
}
```
I expect this unit test to pass:
```
@TestFor(RegenerateCcbJobService)
@Mock([Ccb, Signature])
class RegenerateCcbJobServiceSpec extends Specification {
def setup() {
service.ccbFilerService = Mock(CcbFilerService)
service.executionUtils = new ExecutionUtils(new SyncExecutor())
service.ccbFilerService.fileExporter = Mock(PdfExporter)
}
void "test regenerate CCB PDF"() {
given: "a completed CCB but without document generated"
Ccb ccb = TestHelper.createGrantedCcbContract(1) //The parameter is the ID
ccb.number = 10
ccb.save(flush: true, failOnError: true)
Ccb ccb1 = TestHelper.createGrantedCcbContract(2)
ccb1.number = 20
ccb1.save(flush: true, failOnError: true)
Ccb ccb2 = TestHelper.createGrantedCcbContract(4)
ccb2.number = 40
ccb2.save(flush: true, failOnError: true)
when: "the process is called"
JobResponse response = service.execute("1", "5")
then: "the generate method run one time per ccb"
1 * service.ccbFilerService.generateBaseDocument(ccb)
1 * service.ccbFilerService.generateBaseDocument(ccb1)
1 * service.ccbFilerService.generateBaseDocument(ccb2)
}
}
```
Environment Test config in DataSource.groovy:
```
dataSource {
dbCreate = "create-drop"
driverClassName = "org.h2.Driver"
dialect = "org.hibernate.dialect.H2Dialect"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
username = "******"
password = "******"
}
```
### Actual Behaviour
The unit test fails because the method `Ccb.createCriteria().list` returns a empty list.
Instead, if I filter by number attribute, something like this:
```
Ccb.createCriteria().list {
between("number", 5L, 50L)
projections {
id()
}
}
```
The method returns a list of 3 elements correctly.
And if I filter like this:
```
Ccb.createCriteria().list {
eq("id", 1L)
projections {
id()
}
}
```
The method returns the element correctly!
The problem seems to be when filtering by `ID` and with the `beetwen` clause.
### Steps To Reproduce
_No response_
### Environment Information
- Operating system: macOS
- Java: 8.0.302
### Example Application
_No response_
### Version
2.5.6
Contributor guide
Research direction
Start with the RegenerateCcbJobServiceSpec example and the Ccb.createCriteria().list entry point, then check the test DataSource.groovy configuration using H2. Reproduce the between("id", ...) query with the id projection and compare it with the working eq and number queries. Done means the query returns the expected IDs 1, 2, and 4.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100