向域添加继承时发生错误:org.hibernate.TransientObjectException:object引用未保存的瞬态实例

时间:2014-06-27 19:44:59

标签: hibernate grails gorm

我很难绕过我一直遇到的问题的本质。我的grails应用有两个Domain ClassesHeadOfHouseholdDependent。它们几乎完全相同,除了几个地方;首先,HeadOfHousehold使用构造Dependent拥有static hasMany = [dependents: Dependent]Dependent拥有static belongsTo = [headOfHousehold: HeadOfHousehold],其次Dependent拥有属性String type可以是SpouseDependent,而HeadOfHousehold有一个transient方法getType()只返回Self,最后有两个属性{ {1}}和currentAddress previousAddress也包含在Domain object中,而任何HeadOfHousehold都指向Dependent的地址。现在我无法进行任何类型的查询,这些查询会在HeadOfHousehold上使用Current Address的多个方面,所以我决定通过继承来组合这些对象,这样我就可以查询父类的两者,并从DependentHeadOfHousehold获得结果。

所以我按照以下方式将它们组合在一起: Dependent 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 this is correct place for these, but they deal with // the transformation necessary to create the order object to send it // off to the broker in a byte[]. def getReportDOB(){//dealing with dates is 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 This really shouldn't be necessary I need to figure out why it 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 to 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"
}


}    

我通过直接向构造函数添加地址,然后立即保存地址解决了这个问题 正是如此:

Message: Error executing bootstraps; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ....clients.Address

而不是:

Address currentAddress = FactoryHelperPicardAndPike.createCurrentAddress()
  Address previousAddress = FactoryHelperPicardAndPike.createPreviousAddress()
  HeadOfHousehold hh = FactoryHelperPicardAndPike.createPicard(picardAdded, picardBirthDate, currentAddress, previousAddress)
  saveIt(currentAddress)
  saveIt(previousAddress)
  saveIt(hh)

我不明白究竟是什么问题;以及为什么它只在我添加继承后出现。我只能通过反复试验来解决它,这在我将来遇到问题时并不是很有用。现在我已经阅读了所有可以找到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) 的问题,我能想到的最好的错误表明我的unsaved transient instance对象在被保存之前被视为瞬态,对于某些对象因为它不会与AddressHeadOfHousehold级联保存。我不明白继承对象与独立对象的不同之处。所以任何人都可以解释为什么我正在使用继承对象获取错误,而不是其他方式?此外是某处错误的深入解释?我读过this,这对我来说并不是很有启发性。

我知道这很长,感谢你的关注。

0 个答案:

没有答案