2013-06-11 9 views
1

我有一個域名,其包含車輛 在我的服務類方法,我想從業主的車輛列表中添加一些車輛和刪除一些車輛的列表(所有者)。Grails的獲得方法檢索舊數據

public Owner edit(Long ownerId) { 

    Owner owner 

    Owner.withTransaction { status -> 
     if (ownerId) { 
      def vehicle 
      owner = Owner.get(ownerId) 
      if (owner) { 
       // add vehicles 
       addVechicleList.each { 
        vehicle = Vehicle.get(new Long(it)) 
        if (vehicle) { 
         owner.addToVehicleList(vehicle) 
        } 
       } 

       // remove vehicles 
       removeVehicleList.each { 
        vehicle = Vehicle.get(new Long(it)) 
        if (vehicle) { 
         owner.removeFromVehicleList(vehicle) 
        } 
       } 

       owner?.save(flush: true) 

      } 
    } // end transaction 
    Owner newOwner = Owner.get(ownerId) 
    return newOwner // this has the newly added vehicle but not the removed vehicle. 
} 

域類

Owner.groovy 

class Owner { 
    String ownerName 
    Date dateCreated 
    Date lastUpdated 

    static hasMany = [vehicleList: Vehicle] 

    static constraints = { 
    // some constraints 
    } 

} 

Vehicle.groovy 

class Vehicle { 
    String vehicleName 
    Owner owner 
    Date dateCreated 
    Date lastUpdated 

    static belongsTo = Owner 
    static hasMany = [owners: Owner] 

    static constraints = { 
     // some constraints 
    } 
} 

我保存的數據,我試圖與flush和不flush此外,它是含有被除去的對象。但是,如果我刷新或從UI發出一個新的請求只是獲取所有者的詳細信息,然後它檢索正確的值。保存數據後,我試着通過放置斷點來查看數據庫。數據庫得到更新,但get()方法沒有檢索到正確的值。我正在使用grails 2.2。我在代碼中丟失了什麼?以及爲什麼它沒有采取更新值?

+1

帖子有問題的領域類。 –

+0

@JamesKleeh更新了問題的域類 – user2001627

+0

爲什麼車有一個'owner'和'ownerList'? –

回答

0

嘗試改變Owner類名來別的,我有同樣的問題與Event類幾天前:)

+0

是的,其實我的班級名稱不是所有者只是例如我給這個代碼 – user2001627