Skip to content
Browse files

fix for GPMONGODB-189 "Direct Link support in referenced objects"

  • Loading branch information...
1 parent f1e7895 commit b7fbcb0bb726478ebb77faddda23b1440c127877 @graemerocher graemerocher committed
View
46 ...gorm-mongo/src/test/groovy/org/grails/datastore/gorm/mongo/OneToOneNoReferenceSpec.groovy
@@ -0,0 +1,46 @@
+package org.grails.datastore.gorm.mongo
+
+import grails.gorm.tests.GormDatastoreSpec
+import org.bson.types.ObjectId
+import grails.persistence.Entity
+
+/**
+ */
+class OneToOneNoReferenceSpec extends GormDatastoreSpec{
+
+
+ void "Test that associations can be saved with no dbrefs"() {
+ when:"A domain class is saved that has references disabled"
+ def other = new OtherNoRef().save()
+ def noref = new NoRef(other: other)
+ noref.save flush:true
+
+ then:"The association is saved without a dbref"
+ println NoRef.collection.findOne()
+ NoRef.collection.findOne().other == other.id
+ }
+
+ @Override
+ List getDomainClasses() {
+ [OtherNoRef,NoRef]
+ }
+}
+
+@Entity
+class NoRef {
+
+ ObjectId id
+
+ OtherNoRef other
+
+ static mapping = {
+ other reference:false
+ }
+}
+
+@Entity
+class OtherNoRef {
+
+ ObjectId id
+
+}
View
9 ...store-mongo/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoAttribute.java
@@ -31,6 +31,15 @@
@SuppressWarnings("rawtypes")
private Map indexAttributes;
+ private boolean reference = true;
+
+ public boolean isReference() {
+ return reference;
+ }
+
+ public void setReference(boolean reference) {
+ this.reference = reference;
+ }
@SuppressWarnings("rawtypes")
public Map getIndexAttributes() {
View
36 ...mongo/src/main/groovy/org/grails/datastore/mapping/mongo/engine/MongoEntityPersister.java
@@ -27,13 +27,11 @@
import org.grails.datastore.mapping.engine.EntityAccess;
import org.grails.datastore.mapping.engine.NativeEntryEntityPersister;
import org.grails.datastore.mapping.engine.PropertyValueIndexer;
-import org.grails.datastore.mapping.model.EmbeddedPersistentEntity;
-import org.grails.datastore.mapping.model.MappingContext;
-import org.grails.datastore.mapping.model.PersistentEntity;
-import org.grails.datastore.mapping.model.PersistentProperty;
+import org.grails.datastore.mapping.model.*;
import org.grails.datastore.mapping.model.types.*;
import org.grails.datastore.mapping.mongo.MongoDatastore;
import org.grails.datastore.mapping.mongo.MongoSession;
+import org.grails.datastore.mapping.mongo.config.MongoAttribute;
import org.grails.datastore.mapping.mongo.query.MongoQuery;
import org.grails.datastore.mapping.query.Query;
import org.springframework.context.ApplicationEventPublisher;
@@ -141,6 +139,9 @@ protected List loadEmbeddedCollectionKeys(Association association, EntityAccess
DBRef dbref = (DBRef) o;
keys.add(dbref.getId());
}
+ else if(o != null) {
+ keys.add(o);
+ }
else {
keys.add(null);
}
@@ -394,7 +395,21 @@ protected Object getEntryValue(DBObject nativeEntry, String property) {
protected Object formulateDatabaseReference(PersistentEntity persistentEntity,
@SuppressWarnings("rawtypes") Association association, Serializable associationId) {
DB db = (DB) session.getNativeInterface();
- return new DBRef(db, getCollectionName(association.getAssociatedEntity()), associationId);
+ boolean isReference = isReference(association);
+ if(isReference)
+ return new DBRef(db, getCollectionName(association.getAssociatedEntity()), associationId);
+ else
+ return associationId;
+ }
+
+ private boolean isReference(Association association) {
+ PropertyMapping mapping = association.getMapping();
+ if(mapping != null) {
+ MongoAttribute attribute = (MongoAttribute) mapping.getMappedForm();
+ if(attribute != null)
+ return attribute.isReference();
+ }
+ return true;
}
@Override
@@ -632,11 +647,13 @@ public boolean isDirty(Object instance, Object entry) {
private DBObject nativeEntry;
private Association association;
@SuppressWarnings("hiding") private MongoSession session;
+ private boolean isReference = true;
public MongoAssociationIndexer(DBObject nativeEntry, Association association, MongoSession session) {
this.nativeEntry = nativeEntry;
this.association = association;
this.session = session;
+ this.isReference = isReference(association);
}
public void index(final Object primaryKey, final List foreignKeys) {
@@ -645,9 +662,14 @@ public void index(final Object primaryKey, final List foreignKeys) {
if (!association.isBidirectional()) {
mongoTemplate.execute(new DbCallback<Object>() {
public Object doInDB(DB db) throws MongoException, DataAccessException {
- List<DBRef> dbRefs = new ArrayList<DBRef>();
+ List dbRefs = new ArrayList();
for (Object foreignKey : foreignKeys) {
- dbRefs.add(new DBRef(db, getCollectionName(association.getAssociatedEntity()), foreignKey));
+ if(isReference) {
+ dbRefs.add(new DBRef(db, getCollectionName(association.getAssociatedEntity()), foreignKey));
+ }
+ else {
+ dbRefs.add(foreignKey);
+ }
}
nativeEntry.put(association.getName(), dbRefs);

0 comments on commit b7fbcb0

Please sign in to comment.
Something went wrong with that request. Please try again.