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。我在代碼中丟失了什麼?以及爲什麼它沒有采取更新值?
帖子有問題的領域類。 –
@JamesKleeh更新了問題的域類 – user2001627
爲什麼車有一個'owner'和'ownerList'? –