apache / apache/grails-core

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

Open
#14,382 1 comment 0 reactions 1 assignee Claimed by @puneetbehl View on GitHub
relates-to: grails-data-mongodb
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

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
```groovy
package com.partypeople

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

@Entity
class PersonAttribute {
String id
List 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:
```groovy
package com.partypeople

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

@Service(PersonAttribute)
interface PersonAttributeDataService {

@Where({ contexts { neighborhoodId == neighborhoodId } })
List findByNeighborhoodId(String neighborhoodId)
}
```

```groovy
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 {

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)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.