Permalink
Browse files

Merge pull request #105 from mcholick/1.x

Fix for GPMONGODB-285: Mongo deletes do not cascade in a hasMany/belongs to relationship
  • Loading branch information...
2 parents 4863255 + d405fbb commit 32f2cf71573b0d9eb50ff26a85b7b5b6ce89004d @graemerocher graemerocher committed Mar 18, 2013
@@ -9,16 +9,19 @@ import spock.lang.Issue
class CascadeDeleteSpec extends GormDatastoreSpec {
- @Issue('GPMONGODB-187')
+ @Issue(['GPMONGODB-187', 'GPMONGODB-285'])
void "Test that a delete cascade from owner to child"() {
- given:"An owner with a child object "
+ expect:"No existing user settings"
+ CascadeUserSettings.findAll().isEmpty()
+
+ when:"An owner with a child object is saved"
def u = new CascadeUser(name:"user2")
def s = new CascadeUserSettings()
u.settings = [s] as Set
u.save(flush:true)
- when:"The owner is queried"
+ and:"The owner is queried"
def found1 = CascadeUser.findByName("user2")
def found1a = CascadeUserSettings.findByUser(found1)
@@ -29,11 +32,11 @@ class CascadeDeleteSpec extends GormDatastoreSpec {
when:"The owner is deleted"
found1.delete(flush:true)
def found2 = CascadeUser.findByName("user2")
- def found1b = CascadeUserSettings.findByUser(found1)
+ def allUserSettings = CascadeUserSettings.findAll()
then:"So is the child"
found2 == null
- found1b == null
+ allUserSettings.isEmpty()
}
@Override
@@ -33,6 +33,7 @@
import org.grails.datastore.mapping.engine.AssociationIndexer;
import org.grails.datastore.mapping.engine.EntityAccess;
import org.grails.datastore.mapping.engine.NativeEntryEntityPersister;
+import org.grails.datastore.mapping.engine.Persister;
import org.grails.datastore.mapping.engine.PropertyValueIndexer;
import org.grails.datastore.mapping.model.EmbeddedPersistentEntity;
import org.grails.datastore.mapping.model.MappingContext;
@@ -720,6 +721,19 @@ public Object doInDB(DB con) throws MongoException, DataAccessException {
});
}
+ @Override
+ protected void cascadeDeleteCollection(Collection collection) {
+ Persister persister = null;
+ for (Iterator iter = collection.iterator(); iter.hasNext(); ) {
+ Object child = iter.next();
+ if(persister == null) {
+ persister = session.getPersister(child);
+ }
+ persister.delete(child);
+ iter.remove();
+ }
+ }
+
protected DBObject createDBObjectWithKey(Object key) {
DBObject dbo = new BasicDBObject();
if (hasNumericalIdentifier || hasStringIdentifier) {

0 comments on commit 32f2cf7

Please sign in to comment.