gorm-mongodb icon indicating copy to clipboard operation
gorm-mongodb copied to clipboard

Unable to query embedded objects with @Where annotation on data service

Open amacleay-cohere opened this issue 4 years ago • 0 comments

This issue is WIP - I apologize for the lack of detail at this point. I will be adding to it presently.

I have a simple POGO class that has an embedded property

package com.partypeople

import grails.persistence.Entity
import javax.persistence.Embeddable

@Entity
class PersonAttribute {
  String id
  List<AttributeContext> contexts = []
  static embedded = ['contexts']
}
@Embeddable
class AttributeContext {

    String id
    String neighborhoodId

    static belongsTo = [attribute: PersonAttribute]

}

I would like to generate a query on an embedded field:

package com.partypeople

import grails.gorm.services.Service
import grails.gorm.services.Where

@Service(PersonAttribute)
interface PersonAttributeDataService {

    @Where({ contexts { neighborhoodId == neighborhoodId } })
    List<PersonAttribute> findByNeighborhoodId(String neighborhoodId)
}
package com.partypeople

import grails.test.mongodb.MongoSpec
import grails.testing.gorm.DataTest
import grails.testing.services.ServiceUnitTest

class PatientAttributeDataServiceSpec extends MongoSpec implements DataTest, ServiceUnitTest<PersonAttributeDataService> {

    void "Can do"() {
        given:
        def attribute = new PersonAttribute(contexts: [new AttributeContext(neighborhoodId: '1234')])
        attribute.save()

        when:
        def res = service.findByNeighborhoodId('1234')

        then:
        res.size() == 1
        res[0].contexts.neighborhoodId == '1234'
    }
}

Fails with

java.lang.NullPointerException
	at org.grails.datastore.mapping.model.AbstractPersistentEntity.isIdentityName(AbstractPersistentEntity.java:292)
	at org.grails.datastore.bson.query.BsonQuery.getPropertyName(BsonQuery.java:698)
	at org.grails.datastore.bson.query.BsonQuery.getPropertyName(BsonQuery.java:694)

amacleay-cohere avatar Jan 29 '21 21:01 amacleay-cohere