【发布时间】:2013-06-11 17:18:11
【问题描述】:
我有一个包含车辆列表的域(所有者) 在我的服务类方法中,我尝试添加一些车辆并从所有者的车辆列表中删除一些车辆。
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。我在代码中遗漏了什么吗?为什么它不采用更新的值??
【问题讨论】:
-
发布有问题的域类。
-
@JamesKleeh 用域类更新了问题
-
为什么车辆有
owner和ownerList? -
我想保持一种关系,比如所有者是投资购买车辆的人,并且他已向其他人(如子所有者或用户)授予许可..这更多是业务需求。问题是为什么它没有从 db 获得更新的结果??
标签: grails transactions