2012-10-05 19 views
2

在Grails領域一個已經實現了beforeDelete如下Grails的withNewSession衝不

class Shop { 
    def beforeDelete() { 
     Shop.withNewSession { 
      Client.findAllByShop(this)*.shop = null     
     }  
    } 
} 

但客戶店空值不會保存到數據庫。

如果我添加手動會話沖洗

class Shop { 
    def beforeDelete() { 
     Shop.withNewSession { s2-> 
      Client.findAllByShop(this)*.shop = null   
      s2.flush() 
      s2.clear() 
     } 
    } 
} 

它的工作原理,客戶開店值被置空在數據庫中。

這是一個Grails錯誤還是我誤解了文檔? withNewSession是否意味着自動沖洗?

回答

3

文檔(向下滾動到beforeDelete示例here)似乎意味着不需要刷新或清除會話。

Burt Beckwith已經Grails的郵件列表上也指出(見螺紋here)是手動調用flush()clear()是沒有必要的withNewSession關閉。

說了這麼多,似乎有一個錯誤報告(見細節here)使用withNewSession從Grails 2.2.1開始。

0

withNewSession給你一個新的Hibernate會話,但它不一定是事務性的。這聽起來像你想用withTransaction而不是withNewSession

+0

那麼你建議這樣嗎? 高清beforeDelete(){ Shop.withNewSession {{Shop.withTransaction Client.findAllByShop(本)*店= NULL}} } 我 –

+0

建議,除非你已經嘗試過了,也沒有工作。 – doelleri