The Problem
Having problems persisting your many to many relationships using Lists? Well you've come to the right place!The Classes
We have a many to many relationship between a Player and a Squad, as a squad can have many players, and a player can belong to many squads.We also want to know if the player is active in the squad, so we have another class modelling the relationship, Membership with an active property.
Let's have a quick look at our Domain classes...
We can persist he classes like this...
By default hasMany creates a java.util.Set of Memberships. That's fine, but Sets are unordered, and because I'm a bit fussy, I really want my memberships orders. There are a few ways to achieve ordering (I'm not going to cover them all here). The way I want to do it is by explicitly defining memberships as a List.
The Error
All fine and dandy. Well, actually, no. If we try and persist our domain classes now, we are met with an unexpected (and somewhat incomprehensible) error...org.hibernate.MappingException: Duplicate property mapping of _membershipsIndexBackref found in smitek.fiveasider.Membership
All we did was change our collection from a Set to a List. For me this is a bit harsh, and hopefully something that can be handled in Grails going forward. As you can tell from the error message, this error is occurring down in Hibernate.
After lots of messing around trying various things I finally worked out a way to get this working, with a hint from this link.