Have domain objects: Profile and ProfileProperty. ProfileProperty static belongsTo=Profile
and profile static hasMany=[profileProperties:ProfileProperty]
. Each Profile
has dozen profileProperties
. Need to bulk insert profiles with properties.
Idea was to have Profile#extraProps
of type of java.util.Map
, marked as static transient =['extraProps']
.Overload Profile#afterInsert
and Profile#afterUpdate
and perform something like that:
def afterInsert = {
extraProps.each { k, v ->
new ProfileProperty(name:k, value:v).save()
}
}
This is crucial that Profile and all its ProfileProperties are saved in same transaction. Since speed is important I'm using hibernate batching. Looks like if save()
fails on some property - hibernate throws lots of exceptions saying that session flush should not be performed after exception occurred. I suppose that beforeXXX/afterXXX
hibernate events are wrapped with some magic session/transactions code which is the reason of this problem.
Could someone advise either more elegant solution, or just point me to some code samples that solves similar problem? Thanks in advance.