我有一个包含车辆列表的域(所有者)在我的服务类方法中,我试图添加一些车辆并从所有者的车辆列表中删除一些车辆。
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 刷新或发出新请求,只需获取所有者详细信息,然后它就会检索正确的值。保存数据后,我尝试通过放置断点来查看它的数据库。db 得到了更新,但 get() 方法没有检索到正确的值。我使用的是 grails 2.2。我在代码中遗漏了什么吗?为什么它不采用更新的值?