Oct 29th, 2011, 10:04 AM
list based collection indexes not updated on list entry deletion
I am working with Grails 2.0.0RC1.
I have a domain class, Foo, that has a one-to-many bidirectional owning relationship, bars, with another domain class, Bar. It is backed by a List to be able to maintain ordering.
If I add 3 Foo's to the bars List and then delete the 2nd Bar, the bars_idx for the remaining Bars are not updated properly. The last Bar should get a new bars_idx of 1 but it remains at 2. This causes an NPE when attempting to "show" the Foo. If I fix it by manually changed the bars_idx on the last Bar via sql, the Foo then shows correctly.
This seems related to code that fixed GRAILS-3783 issue. That fix may have made the docs in Section 5.2.4 on Lists Of Objects somewhat obsolete or in need of clarification. I find that I do not have to manually add/remove the Bar from the List in the Controller code as I thought I would have to. If I actually do that in the Controller code, it messes up the List state.
I also wrote some code to maintain the List ordering, i.e. Up/Down/Top/Bottom. Interestingly enough I noticed that I merely had to manipulate the List to get it to work using the set method. I did not have to call save() to get the index changes to persist.I'm not sure at this point how this happens but I imagine there's some sort of OpenSessionInView filter t
Nov 10th, 2011, 12:44 PM
The answer is that you have to manually remove the Bar from the Foo before deleting the Bar, i.e. in the controller's delete method.
It is not necessary to add it in manually to the list as described in the documentation.