2012-10-02 26 views
0

我經常在Grails中做錯事情並獲取未保存的瞬態實例異常。我不能粘貼代碼,如果我做了,你可能會告訴我停止編程永遠。但這裏是通用的情況下,我想一些幫助:Grails/GORM/Hibernate:引用未保存的瞬態實例的域實例的屬性列表

我打電話instanceOfMyComplicatedDomainClass.save(flush: true, failOnError: true)和Grails /休眠給我無處不在:

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中是否有任何方法可以做類似的事情?

我問不同的東西從我的其他問題,列出所有未保存的瞬變無處不在:Get a list of all objects grails is planning to magically save

另外,我看到(並讀取)〜15等「休眠:未保存的短暫......」的問題,我正在尋求一種通用的解決方案來查看問題所在,而不是針對當前特定片段中所做錯誤的具體解決方案。教一個人去釣魚...可以這麼說。

+0

哇,我真的很拙劣的標題就制定好SEO博鏈接 – Mikey

+1

取出failOnErrorrs和打印domainInstance.errors調用save()後 – Raphael

+0

我想了一秒鐘,我忽視的東西那麼明顯,但其同樣的消息,並且我仍然無法確定哪個屬性是問題所在。 – Mikey

回答

2

Burt今天發佈了一些有趣的東西。

www.burtbeckwith.com/blog/?p=1570

結帳休眠聽衆。

相關問題