From c06aae41bd45ec7ba527c11c9faa15975f0cbf26 Mon Sep 17 00:00:00 2001 From: Andrew Kenneth MacLeay Date: Fri, 29 Jan 2021 19:01:42 -0500 Subject: [PATCH 1/3] WIP add a test demonstrating issue See https://github.com/grails/gorm-mongodb/issues/195 Note: test is not currently functional: failing to instantiate data service for test. --- .../gorm/mongo/EmbeddedWhereClauseSpec.groovy | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy diff --git a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy new file mode 100644 index 00000000..75df5c64 --- /dev/null +++ b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy @@ -0,0 +1,57 @@ +package org.grails.datastore.gorm.mongo + +import grails.gorm.services.Service +import grails.gorm.services.Where +import grails.gorm.tests.GormDatastoreSpec +import grails.persistence.Entity +import org.springframework.beans.factory.annotation.Autowired + +import javax.persistence.Embeddable + +class EmbeddedWhereClauseSpec extends GormDatastoreSpec { + + @Autowired + PersonAttributeDataService personAttributeDataService + + void "Can construct data service where clause on embedded object"() { + given:"An object with an embedded field on it" + def attribute = new PersonAttribute(contexts: [new AttributeContext(neighborhoodId: '1234')]) + attribute.save() + + when:"We query using the autogenerated where clause" + def response = personAttributeDataService.findByNeighborhoodId('1234') + + then:"The association is valid" + response.size() == 1 + response.first().contexts.first().neighborhoodId == '1234' + + } + + @Override + List getDomainClasses() { + [PersonAttribute] + } +} + +@Entity +class PersonAttribute { + String id + List contexts = [] + static embedded = ['contexts'] +} + +@Embeddable +class AttributeContext { + + String id + String neighborhoodId + + static belongsTo = [attribute: PersonAttribute] + +} + +@Service(PersonAttribute) +interface PersonAttributeDataService { + @Where({ contexts { neighborhoodId == neighborhoodId } }) + List findByNeighborhoodId(String neighborhoodId) +} From 4d1a65b253ed58e3e468dd89788673769997093a Mon Sep 17 00:00:00 2001 From: Andrew Kenneth MacLeay Date: Fri, 29 Jan 2021 19:23:27 -0500 Subject: [PATCH 2/3] WIP: closer but still wrong --- grails-datastore-gorm-mongodb/build.gradle | 1 + .../datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/grails-datastore-gorm-mongodb/build.gradle b/grails-datastore-gorm-mongodb/build.gradle index d79fe5b6..61d52a14 100644 --- a/grails-datastore-gorm-mongodb/build.gradle +++ b/grails-datastore-gorm-mongodb/build.gradle @@ -14,6 +14,7 @@ dependencies { compileOnly "javax.servlet:javax.servlet-api:$servletApiVersion" testImplementation "org.grails:grails-datastore-gorm-tck:$datastoreVersion" + testImplementation "org.grails:grails-gorm-testing-support:${testingSupportVersion}" testImplementation "org.hibernate:hibernate-validator:$hibernateValidatorVersion" testImplementation "org.grails:grails-datastore-gorm-support:$datastoreVersion", { exclude group: "org.grails", module:"grails-datastore-gorm-hibernate-core" diff --git a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy index 75df5c64..914e9bf7 100644 --- a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy +++ b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy @@ -4,14 +4,12 @@ import grails.gorm.services.Service import grails.gorm.services.Where import grails.gorm.tests.GormDatastoreSpec import grails.persistence.Entity -import org.springframework.beans.factory.annotation.Autowired +import grails.testing.services.ServiceUnitTest import javax.persistence.Embeddable -class EmbeddedWhereClauseSpec extends GormDatastoreSpec { +class EmbeddedWhereClauseSpec extends GormDatastoreSpec implements ServiceUnitTest { - @Autowired - PersonAttributeDataService personAttributeDataService void "Can construct data service where clause on embedded object"() { given:"An object with an embedded field on it" @@ -19,7 +17,7 @@ class EmbeddedWhereClauseSpec extends GormDatastoreSpec { attribute.save() when:"We query using the autogenerated where clause" - def response = personAttributeDataService.findByNeighborhoodId('1234') + def response = service.findByNeighborhoodId('1234') then:"The association is valid" response.size() == 1 From 9ec044ca91b684053dc3749666182bbebc223df3 Mon Sep 17 00:00:00 2001 From: jameskleeh Date: Fri, 26 Feb 2021 12:15:45 -0500 Subject: [PATCH 3/3] Make the test pass --- .../mapping/mongo/config/MongoMappingContext.java | 9 ++++++++- .../datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoMappingContext.java b/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoMappingContext.java index 146d1576..df73c16f 100644 --- a/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoMappingContext.java +++ b/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoMappingContext.java @@ -375,16 +375,23 @@ public PersistentEntity createEmbeddedEntity(Class type) { class DocumentEmbeddedPersistentEntity extends EmbeddedPersistentEntity { - private DocumentCollectionMapping classMapping ; + private DocumentCollectionMapping classMapping; + public DocumentEmbeddedPersistentEntity(Class type, MappingContext ctx) { super(type, ctx); classMapping = new DocumentCollectionMapping(this, ctx); } + @Override + public boolean isIdentityName(String propertyName) { + return false; + } + @Override public ClassMapping getMapping() { return classMapping; } + public class DocumentCollectionMapping extends AbstractClassMapping { private Collection mappedForm; diff --git a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy index 914e9bf7..1dbc2aef 100644 --- a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy +++ b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/EmbeddedWhereClauseSpec.groovy @@ -8,13 +8,15 @@ import grails.testing.services.ServiceUnitTest import javax.persistence.Embeddable -class EmbeddedWhereClauseSpec extends GormDatastoreSpec implements ServiceUnitTest { - +class EmbeddedWhereClauseSpec extends GormDatastoreSpec { void "Can construct data service where clause on embedded object"() { given:"An object with an embedded field on it" def attribute = new PersonAttribute(contexts: [new AttributeContext(neighborhoodId: '1234')]) attribute.save() + session.flush() + session.clear() + def service = session.datastore.getService(PersonAttributeDataService) when:"We query using the autogenerated where clause" def response = service.findByNeighborhoodId('1234') @@ -22,7 +24,6 @@ class EmbeddedWhereClauseSpec extends GormDatastoreSpec implements ServiceUnitTe then:"The association is valid" response.size() == 1 response.first().contexts.first().neighborhoodId == '1234' - } @Override