Grails/Groovy 不会在对象的集合中持续更改

标签 grails groovy persistence

我有以下问题:

我的对象路线包含路线点列表。当我更改此列表时,更改的列表将被保存。但是当下一个方法访问列表时,更改似乎恢复了。我在更改方法结束时排除了某种事务回滚,b/c,我通过从数据库加载列表来访问列表,它仍然具有正确的(更改后的)大小。这是代码:

首先是修改方法:

def removeStationFromRoute(station){
    def driver = Driver.get(Integer.valueOf(requestAccessService.getParams().userId))
    def route = driver.routes.find{
        it.routeDate == new Date().clearTime()
    }
    def rp = Routepoint.findByStation(station)

    route.routepoints.remove(rp)

    def newRoute = driver.routes.find{ it.routeDate == new Date().clearTime()}
    println 'new route size: ' + newRoute.routepoints.size()

    def newRoute2 = Route.get(route.id)
    println 'new route from db size: ' + newRoute2.routepoints.size()

}

两次打印都返回 5 的大小,这是正确的。执行此方法后,立即执行此方法:
def getDriverRoute(){
    def driver = User.get(Long.valueOf(params.userId))
    def route = driver.routes.find{ it.routeDate == new Date().clearTime()}
    println 'serialized route size: ' + route.routepoints.size()
    def routeString = jobService.serializeRoute(route)
    log.info('Route with ' + route.routepoints.size() + " stations serialized for User " + driver.encodeAsHTML())
    render routeString
}

打印大小为 6,就好像列表没有发生变化一样。我已经尝试在“removeStationFromRoute”列表中进行更改后保存驱动程序、路线和路线点,并检查三个对象是否有错误。没有帮助。

感谢您提供任何想法该怎么做!

最佳答案

我猜你有 Route 之间的 1-N 关系和 Routepoints ?就像是

class Route {
  static hasMany = [routepoints: Routepoint]
}

class Routepoint {
  static belongsTo = [route: Route]
}

不应该 使用 add 添加/删除路由点/remove Collection 的方法界面。相反,您应该使用 addTo*/removeFrom* GORM 方法,例如
route.addToRoutepoints(routepoint)
route.removeFromRoutepoints(routepoint)

关于Grails/Groovy 不会在对象的集合中持续更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056718/

相关文章:

security - 使用 spring-security-core-ldap 插件在 grails 中实现 LDAP 身份验证?

hibernate - 创建子项目后,缓存关联不会更新

maven-2 - Maven依赖项解析

grails - Grails运行应用程序问题ERROR context.ContextLoader

Grails 未加载 .jar 文件中的类

java - Collection 仅添加,从未阅读

java - Java 中的持久数据结构

selenium - 执行 Selenium 显式等待时 Katalon 中的 GroovyCastException

Grails[Groovy],如何获取一个类没有继承的所有方法的列表?

hibernate 映射异常 : Repeated column in mapping for entity