4

I am using Grails and am quite surprised by the way hasMany relationships work. I have a typical hasMany relationship where the parent id is in the child table. When I insert a child and try to save it through the parent object, the parent object's version id gets incremented. My question is: Why should the parent's version id change when there is a change only in the child object?

class Parent { static hasMany = [children: child] } class child { string name Parent parent static belongsTo = [Parent] } def p = Parent.get(1) p.addToChildren(new Child(name: "Roy")) p.save() 

The version of p gets incremented from 0 to 1. Is there any way I can avoid this in Grails?

Due to the change in version ID of the parent I get a stale object exception. Any help?

2
  • In my mind, the Parent object has changed, because it got a new child. My 2c worth Commented Sep 13, 2010 at 9:07
  • Soren is correct - a property has changed (analagous to adding one char to a String property) so the whole object is considered changed. This is a Hibernate thing and not a Grails thing. Commented Sep 13, 2010 at 13:44

1 Answer 1

2

One possibility is to disable optimistic locking for your domain object.

Update

Or try to search.

Sign up to request clarification or add additional context in comments.

1 Comment

is there an other solution? I need to have a version id column in the table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.