2013-09-22 41 views
3

我目前正在研究一個grails應用程序,並且我有一個連接到一個帳戶的地址列表。基本上我想要做的是當賬戶被編輯時顯示所有連接地址的當前列表,然後我可以從視圖中刪除/添加儘可能多的數據。當這個數據被捕獲時,它被控制器拾取,我想要做的是能夠清除該賬戶中的所有當前地址,然後再次使用視圖中存在的內容創建列表,我的代碼如下:Grails清除hasMany條目並添加新的錯誤?

帳戶域:

class Account { 

    String name 
    Date dateCreated 
    Date lastUpdated 

    static hasMany = [addresses:Addresses] 

    static mapping = { 
     addresses cascade:"all-delete-orphan" 
    } 

    def getAddressesList() { 
     return LazyList.decorate(
       addresses, 
       FactoryUtils.instantiateFactory(Addresses.class)) 
    } 

    static constraints = { 
     name(blank:false, unique: true) 
    } 

} 

地址域:

class Addresses { 

    int indexVal 
    String firstLine 
    String postcode 
    String area 

    static belongsTo = [account:Account] 

    static mapping = { 
    } 

    static transients = [ 'deleted' ] 

    static constraints = { 
     indexVal(blank:false, min:0) 
    } 


} 

帳戶控制:

def update() { 

    def accountInstance = Account.get(params.id) 
    if (!accountInstance) { 
     flash.message = message(code: 'default.not.found.message', args: [message(code: 'account.label', default: 'Account'), params.id]) 
     redirect(action: "list") 
     return 
    } 

    if (params.version) { 
     def version = params.version.toLong() 
     if (accountInstance.version > version) { 
      accountInstance.errors.rejectValue("version", "default.optimistic.locking.failure", 
         [message(code: 'subscriptions.label', default: 'Subscriptions')] as Object[], 
         "Another user has updated this Account while you were editing") 
      render(view: "edit", model: [accountInstance: accountInstance]) 
      return 
     } 
    } 

    accountInstance.properties = params 

    accountInstance.addresses.clear() 
    accountInstance.save(flush: true) 

    .... 

} 

錯誤:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses. Stacktrace follows:
Message: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses

這個錯誤似乎將控制器發生在行:

accountInstance.save(flush: true) 

我已經嘗試了幾種不同的方式來得到這個工作,真的很感謝一些幫助。

+1

在'params'設置爲'properties'之前不應該清除()'?交換這兩行。 – dmahapatro

+0

我試過了,但仍然得到相同的錯誤:S – user723858

+0

如果我從帳戶域中刪除該行(地址級聯:「all-delete-orphan」),然後讓控制器清除地址並添加新的地址,我不會得到一個錯誤,但它只是將它們添加到那裏已經創建一些重複,任何想法? – user723858

回答

7

因此,您似乎已經完成了Grails可以爲您做的一些工作。

class Account { 

    String name 
    Date dateCreated 
    Date lastUpdated 

    List addresses 

    static hasMany = [addresses:Address] 

    static mapping = { 
     addresses cascade:"all-delete-orphan" 
    } 

    static constraints = { 
     name(blank:false, unique: true) 
    } 

} 

class Address { 
    String firstLine 
    String postcode 
    String area 

    static belongsTo = [account:Account] 
} 

這會產生你想讓地址成爲列表的效果。

我發現無論是

instance.addresses = null 

instance.addresses.clear() 

爲我

-1

工作,當你定義addresses cascade:"all-delete-orphan"Account類,你不Addresses需要static belongsTo = [account:Account]。所以試着刪除那個語句並測試你的代碼。請參閱相關的link

+2

belongsTo和級聯策略與彼此無關。 –