Grails/GORM : org.hibernate.AssertionFailure: null id in xyz (don't flush the Session after an exception occurs) -


i've grails/jaxrs app fails persist nested object graph automatically, , wondering if there datamodel make work.

the resource deserializes object properly, save parent object (tauthor), fail save children (tooks) automatically. children have null ids, null references parent.

i can manually create child objects, looking better way manage this.

domain classes

class tauthor {      string nameshort     integer age      static hasmany = [tooks:took]  }  class took {      string title;      static belongsto = [tauthor:tauthor] } 

resource

@consumes([mediatype.application_json, "application/json"]) @produces([mediatype.application_json, "application/json"]) @path('/api/tauthor') class tauthorresource {      tauthorservice tauthorservice      @post     tauthor create(tauthor dto) {       tauthor created = tauthorservice.save(dto)       if(!created.haserrors()) {         return created       }     } } 

desired, broken service

class tauthorservice {      tauthor save(tauthor dto) {         dto.validate()          if (dto.haserrors()) {             return dto         }          return dto.save()     } } 

working service

class tauthorservice {      tauthor save(tauthor dto) {         dto.validate()          if (dto.haserrors()) {             return dto         }          // remove tooks, , create them separately         set<took> tooks = []         tooks += dto.tooks          tooks.each { took ->             dto.removefromtooks(took)         }          tauthor created = dto.save()          // readd tooks tauthors         tooks.each { took->             took.tauthor = created             took.save()         }          tooks.each { took->             created.addtotooks(took)         }         created.save()          return created      } } 

example json

{    "class":"org.tan.tauthor",    "nameshort":"tankak",    "age":13,    "tooks":[       {          "class":"org.tan.took",          "title":"harry"       },       {          "class":"org.tan.took",          "title":"potter"       }    ] } 


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -