hibernate - Grails 3 - domain object with composite key not always saving -
we upgraded grails 2.2.4 3.2.2. since then, we've been having issues when trying persist newly created object composite key.
let's have domain object called somefield.
class somefield { static belongsto = [parentclass : someparentclass] static hasmany = [joinedfields : joinedfield] joinedfields fetch:'join', cascade:'all-delete-orphan' }
there's domain object called joinedfield belongsto 1 of 2 somefield members. uses both fields create composite key.
class joinedfield { somefield field1 somefield field2 static belongsto = [field1: somefield] static mapping = { id composite: ['field1', 'field2'] columns { field1 column:'f1_id' field2 column:'f2_id' } version false } static contraints = { field1(nullable:false) field2(nullable:false) } def getpk = { ['field1':field1.id, 'field2':field2.id] } }
field2 exists in database , gets looked there when adding joinedfield object.
when field1's parent new , save it, field1 saved , joinedfield object persisted 2 keys expected. when field1's parent has been saved, however, , joinedfield object added existing field1 object or new field1 object , saved, joinedfield object not persisted.
example:
someparent.addtofields( arealfield1 ) def jf = new joinedfield() arealfield1.addtojoinedfields( jf ) jf.field2 = somefield.findbyid( 1234 ) someparent.save()
so -- if arealfield1 belongs parent not yet saved, 'arealfield1' , 'jf' persisted when arealfield1's parent saved. if field1's parent exists, 'jf' not saved when arealfield1's parent saved, though arealfield1 saved fine.
i assume gorm not recognizing object dirty , needing persistence because it's created 2 existing objects, though new object made of 2 existing objects. don't see way force cascading save happen in case. working grails 2.2.4.
update:
it seems there transaction issue @ heart of this. have controller someparentclass request received save object. in turn calls transactional service class within 'withtransaction' closure allow roll transaction if errors occur. service numerous saves , want keep ability roll whole set on error.
if remove @transactional annotation on service, , withtransaction closure in controller, joinedfield class above saves fine. however, lacking rollback of complete set of changes when error occurs.
we have removed withtransaction in controller , can handle rollback in service when still annotated transactional, joinedfield object still fails persist described.
example: code rollback in service cleanest:
import grails.transaction.transactional @transactional class buildobjectservice { def buildobject(def json) { try { // build object json representation } catch( exception e ) { transactionstatus.setrollbackonly() throw e } } }
any ideas on why fails persist when within transactional service fine when annotation removed?
resolved issue upgrading org.grails:grails-datastore-gorm*, org.grails:grails-datastore-core , org.grails.plugins:hibernate5 latest 6.1.6.release version.
this replaces 6.0.3 versions org.grails:grails-depenedencies pulling in.
Comments
Post a Comment