我一直在做错事,并在Grails中得到未保存的瞬时实例异常。我无法粘贴代码,如果这样做,您可能会告诉我永远停止编程。但是这是我需要一些帮助的一般情况:
我叫instanceOfMyComplicatedDomainClass.save(flush: true, failOnError: true)
,grails / hibernate让我无处不在:
object references an unsaved transient instance - save the transient instance before flushing: soexample.SomeOtherDomainClass
考虑到我有几个相同的类,我该怎么做(除了理解我自己的代码),以查看哪个属性是问题?我想要一个可以在
.save()
之前调用的方法,该方法将导致println
/ log
/引起该问题的所有名称。让我举个例子:class MyComplicatedDomainClass {
SomeOtherDomainClass dataContents
SomeOtherDomainClass moreDataContents
static constraints = {
dataContents(nullable:true)
moreDataContents(nullable:true)
}
}
class SomeOtherDomainClass {
String randomData
}
...
def someRandomMethodAnywhere(){
def newComplicated = new MyComplicatedDomainClass()
def imUnsaved = new SomeOtherDomainClass(randomData:'lalala')
def imOK = new SomeOtherDomainClass(randomData:'lalala2').save()
newComplicated.dataContents = imUnsaved
newComplicated.moreDataContents = imOK
//At this point newComplicated references an unsaved transient, "imUnsaved". If I try to save newComplicated it will fail.
def badPropertyList = findUnsavedTransients(newComplicated)
assert badPropertyList.contains("dataContents") //So findUnsavedTransients method returns a list of the names of the properties referencing the unsaved transients
}
我将如何编写findUnsavedTransients? Hibernate中是否已有任何可以做类似事情的方法?
我要问的问题与其他问题不同,它列出了所有未保存的 transient 信号:Get a list of all objects grails is planning to magically save
另外,我看到(并且已经阅读)其他约15个“休眠:未保存的 transient ...”问题,我正在寻求一个通用的解决方案以查看问题所在,而不是针对我做错的事情的具体解决方案在今天的特定片段中教一个人钓鱼...可以这么说。
最佳答案
伯特今天发表了一些有趣的话。
www.burtbeckwith.com/blog/?p=1570
check out 休眠的侦听器。
关于hibernate - Grails/GORM/Hibernate:引用未保存的 transient 实例的域实例的属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682787/