2014-04-22 59 views
1

我有一個命令對象,看起來如下...Grails的域對象總是保存

@Validateable 
class ACommand implements Serializable 
{ 
    ADomainObject bundleDef 
} 

我用一個表格填寫命令,然後使用類似

def save(ACommand command) 
控制器

我還添加以下到我的Config.groovy中

grails.gorm.autoFlush = false 

的問題是,即使我不沖水(甚至CAL l .save())它在返回時仍然保存到數據庫中。有沒有人經歷過這個?有沒有辦法解決它?

更新

也試過以下

def save(ACommand command) 
{ 
    try{ 
     service.saveADomainObject(command.adomain) //save called in here if no error 
    } 
    catch(Exception e3){ 
     log.error(e3); 
     command.adomain.discard() 
    } 
    // renders ... 
} 

這也不起作用,即使丟棄叫(我設置一個斷點),它仍然保存。

更新2

我改變了我的服務來查找如下

adomain.discard() 
throw new InvalidObjectException("Blah Blah Blah") 

這似乎保存到數據庫拋出錯誤後直接。我也確認我的服務是交易性的。

更新3

添加我的服務類參考

​​

回答

0

這是我設法得到它的工作...

首先在服務我加入基於該qoute

@Transactional(readOnly = true)註釋該版本默認爲是讀 - 寫事務(由於類級註釋)的所有方法,但listBooks甲基od覆蓋這個使用只讀交易:

這有幫助,但現在它不會保存期限。所以,我在圖克.save()調用出來讓我的控制器看起來就像這樣......

def save(ACommand command) 
{ 
    try{ 
    service.saveADomainObject(command.adomain) //save called in here if no error 
    command.adomain(flush:true) 
    } 
    catch(Exception e3){ 
    log.error(e3); 
    } 
    // renders ... 
} 

我需要衝洗:真正儘管readding自動沖洗,所以我不知道有。

1

如果ADomainObject bundleDef綁定到一個對象,它已經是持久的(即,如果結合此對象從數據庫加載的記錄),那麼在Hibernate會話關閉時,對該對象的更改將自動保存到數據庫中。

假設你不想保存對象,如果約束是無效的做到這一點:

def save(ACommand command) { 

    if (!command.adomain.save()) { 
     log.error "Failed to save domain due to errors $command.adomin.errors" 
     // other error handling - maybe show a form that allows the user to correct the errors 
    } else { 
     // object saved successfully, redirect to homepage, throw a party, or whatever 
    } 
} 
+0

我嘗試這樣做,它不工作我可以更新我的代碼是否有幫助 – Jackie

+0

如果你顯示控制器的行動,應有助於 –

+0

加入要求提供的信息讓我知道如果你需要更多 – Jackie

相關問題