Skip to content

Commit

Permalink
GRAILS-9170 Fix cascading saves on multiple levels
Browse files Browse the repository at this point in the history
Cascading saves were broken for "multiple levels" with Hibernate 2nd level cache disabled
and using some other id generator than 'identity'.
  • Loading branch information
lhotari committed Jun 7, 2012
1 parent 3cc9e52 commit 635189f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Expand Up @@ -271,7 +271,7 @@ protected Serializable performSaveOrReplicate(Object entity, EntityKey key, Enti
if (!useIdentityColumn) { if (!useIdentityColumn) {
EntityInsertAction insert = new EntityInsertAction(id, values, entity, version, persister, source); EntityInsertAction insert = new EntityInsertAction(id, values, entity, version, persister, source);
source.getActionQueue().execute(insert); source.getActionQueue().execute(insert);
if(!source.getPersistenceContext().wasInsertedDuringTransaction(persister, id)) { if(persister.hasCache() && !source.getPersistenceContext().wasInsertedDuringTransaction(persister, id)) {
insertVetoed=true; insertVetoed=true;
} }
} }
Expand Down
@@ -0,0 +1,75 @@
package org.codehaus.groovy.grails.orm.hibernate

class CascadingSaveAndNonIdentityGeneratedIdTests extends AbstractGrailsHibernateTests {

protected void onSetUp() {
gcl.parseClass '''
package cascadingsave
import grails.persistence.Entity
@Entity
class Request {
static hasMany = [samples: Sample]
Date dateCreated
Date lastUpdated
static mapping = {
cache false
id generator: 'increment'
}
}
@Entity
class Sample {
static belongsTo = [request : Request]
static hasMany = [attributes: Attribute]
static mapping = {
cache false
id generator: 'increment'
}
}
@Entity
class Attribute {
static belongsTo = [sample : Sample]
static mapping = {
cache false
id generator: 'increment'
}
}
'''
println "Parsed!"
}

void testCascadingSaveToMultipleLevels() {
def requestClass = ga.getDomainClass("cascadingsave.Request").clazz
def sampleClass = ga.getDomainClass("cascadingsave.Sample").clazz
def attributeClass = ga.getDomainClass("cascadingsave.Attribute").clazz

def request = requestClass.newInstance()

10.times {
def sample = sampleClass.newInstance()
request.addToSamples(sample)
10.times {
sample.addToAttributes(attributeClass.newInstance())
}
}

request.save(flush:true)

requestClass.withNewSession {
def savedRequest = requestClass.get(request.id)
assert savedRequest.samples.size() == 10
savedRequest.samples.each {
assert it.attributes.size() == 10
}
}


}
}

0 comments on commit 635189f

Please sign in to comment.