0

I have the following problem:

My object route contains a list of routepoints. When I alter this list, the changed list is saved. But when the next method accesses the list, it seems like the change reverted. I ruled some kind of transaction rollback out, b/c on the end of the altering method, i acces the list by loading it from the database and it still has the right (altered) size. Here's the code:

First the altering method:

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()

}

Both prints return a size of 5, which is correct. Right after this method is carried out, this method is executed:

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
}

Which prints a size of 6, as if no change happened to the list. I already tried saving the driver, the route and the routepoint after the change is made in the "removeStationFromRoute"-List, as well as checking the three objects for errors. Didn't help.

Thanks for any ideas what to do!

olkoza
  • 715
  • 2
  • 17
  • 35

2 Answers2

1

I guess you have a 1-N relationship between Route and Routepoints? Something like

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

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

You should not add/remove routpoints using the add/remove methods of the Collection interface. Instead you should use the addTo*/removeFrom* GORM methods, e.g.

route.addToRoutepoints(routepoint)
route.removeFromRoutepoints(routepoint)
Dónal
  • 185,044
  • 174
  • 569
  • 824
0

Firstly, After you have used route.removeFromRoutepoints(routepoint) to remove the mapping of the Routepoint with Route in the first method, the Route Object Still needs to be persisted using .save/.merge method.(Check here )

Secondly. In hibernate, using Domain.get(id) will not always hit the Database, IF the object already cached in the Hibernate session. Check here

Hope it helps...

Community
  • 1
  • 1
DKanavikar
  • 31
  • 5