NPE with hasMany sort field mapping
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
When we configure hasMany property to be sorted by a sub-property, its throwing an NPE. This confguration was working fine in grails-2.
```groovy
class Person {
String firstName
String lastName
static hasMany = [devices: PersonDevice]
static mapping = [
devices cascade: 'all-delete-orphan', sort: 'device.priority'
]
}
class Device {
String name
Integer priority
}
class PersonDevice {
Device device
Date purchaseDate
}
```
Here i would like sort the **person.devices** by **device.priority**. Getting the following exception on grails app startup.
```
Caused by: java.lang.NullPointerException: null
at org.grails.orm.hibernate.cfg.GrailsDomainBinder.bindCollectionSecondPass(GrailsDomainBinder.java:372)
at org.grails.orm.hibernate.cfg.GrailsDomainBinder$GrailsCollectionSecondPass.doSecondPass(GrailsDomainBinder.java:3399)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1631)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1599)
```
I suggest to do the following:
- Add a defensive null check against the propertyToSortBy and throw an appropriate error.
- Add support for sorting by the properties of the relation.
**propertyToSortBy** is returned as null, as it is looking up property by name.
https://github.com/grails/gorm-hibernate5/blob/62ba190ee385b99f8a4e551790506dbf235a000b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java#L362
Contributor guide
Research direction
Start in grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java around the propertyToSortBy lookup at line 362 and the bindCollectionSecondPass stack-trace location. Reproduce the Person.devices mapping during Grails startup, then verify that it either reports an appropriate error or supports sorting by device.priority without an NPE.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100