Grails get方法检索旧数据

时间:2013-06-11 17:18:12

标签: grails transactions

我有一个域名(所有者),其中包含车辆列表 在我的服务类方法中,我试图添加一些车辆并从车主列表中删除一些车辆。

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 :(得分:0)

尝试将Owner类名更改为其他任何内容,几天前我遇到与Event类相同的问题:)