grails - Error occurs when adding inheritance to domain: org.hibernate.TransientObjectException: object references an unsaved transient instance -
i'm having difficulty wrapping head around nature of problem i've been having. have 2 domain classes grails app: headofhousehold , dependent. identical, except in couple of places; firstly, headofhousehold owns dependent using construct: static hasmany = [dependents: dependent], , dependent has static belongsto = [headofhousehold: headofhousehold], secondly dependent has property string type can either spouse or dependent while headofhousehold has transient method gettype() returns self, , lastly there 2 properties currentaddress , previousaddress domain objects contained in headofhousehold, while dependent points headofhousehold's addresses. having trouble doing sort of query use more 1 aspect of current address on dependent decided combine these objects through inheritance query vs parent class of two, , results both headofhousehold , dependent.
so combined them in following way: parent class person
class person { string firstname string lastname string middlename string suffix date dateofbirth date dateadded string driverslicensenumber string driverslicensestate string ssn string gender address currentaddress address previousaddress mvr mvr creditreport creditreport clueauto clueauto clueproperty clueproperty static transients =['reportdob','reportdlnumberstate','firstlastmiddlesuffix', 'reportaddressstring'] static constraints = { middlename blank: false, nullable: true suffix blank: true, nullable: true ssn matches: "[0-9]{9}" gender blank: true, nullable: true, matches: "m|f" currentaddress nullable: true previousaddress blank: true, nullable: true mvr nullable: true creditreport nullable: true clueauto nullable: true clueproperty nullable: true dateadded nullable: true } public string tostring(){ string returnvalue = lastname + ", " + firstname + " " + middlename return returnvalue.replaceall("null"," ") } // i'm not sure if correct place these, deal // transformation necessary create order object send // off broker in byte[]. def getreportdob(){//dealing dates stupid in java x_x if(dateofbirth){ gregoriancalendar calendar = new gregoriancalendar() calendar.settime(dateofbirth) return datatypefactory.newinstance().newxmlgregoriancalendar(calendar) } } def getreportdlnumberstate(){ return driverslicensenumber +"_" + driverslicensestate } def getfirstlastmiddlesuffix(){ return (firstname+"_"+lastname+"_"+middlename+"_"+suffix).replaceall("null","") } def getreportaddressstring(){ if(!previousaddress.isempty()){ return "0_home,1_business" } else{ return "0_home" } } //todo shouldn't necessary need figure out why is. def convertgender(){ switch(gender){ case "m": return "m" case "f": return "f" default: return null } } } child class dependent
class dependent extends person{ string type //relationship head of household. static belongsto = [headofhousehold: headofhousehold] static constraints = { } public string tostring(){ string returnvalue = lastname + ", " + firstname + " " + middlename return returnvalue.replaceall("null","") } def beforeinsert(){ currentaddress = headofhousehold.currentaddress previousaddress = headofhousehold.previousaddress dateadded = new date() } }
child class headofhousehold
class headofhousehold extends person{ static hasmany = [dependents: dependent] static transients =['type'] static mapping = { } static constraints = { } def gettype(){ return "self" } } now, works great, except had major problems instantiating objects. kept getting error:
message: error executing bootstraps; nested exception org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: ....clients.address which resolved adding addresses directly constructor, , saving addresses afterwards thusly:
address currentaddress = factoryhelperpicardandpike.createcurrentaddress() address previousaddress = factoryhelperpicardandpike.createpreviousaddress() headofhousehold hh = factoryhelperpicardandpike.createpicard(picardadded, picardbirthdate, currentaddress, previousaddress) saveit(currentaddress) saveit(previousaddress) saveit(hh) instead of:
headofhousehold hh2 = factoryhelperpicardandpike.createpike(pikeadded, picardbirthdate) address currentaddress2 = factoryhelperpicardandpike.createpikecurrentaddress() address previousaddress2 = factoryhelperpicardandpike.createpikepreviousaddress() hh2.setcurrentaddress(currentaddress2) hh2.setpreviousaddress(previousaddress2) saveit(currentaddress2) saveit(previousaddress2) saveit(hh2) i don't understand problem is; , why appeared after added inheritance. able resolve via trial , error, isn't helpful when run problem in future. i've read questions find referring unsaved transient instance, , best can figure error indicates address object considered transient until saved, , reason won't cascade save headofhousehold or dependent. don't understand how different inherited object vs stand alone object. can explain why i'm getting error inherited objects, , not other way? furthermore in depth explanation of error somewhere? i've read this, , isn't illuminating me.
i know long, , appreciate attention.
Comments
Post a Comment