I was wondering your thoughts on the best way to implement a SQL ManyToMany relationship in Java using annotations – in this case eBeans – where there is extra data associated with the join.
I have created a db diagram to help explain:
Using @ManyToMany on the organisation and users classes would create the join table but without the extra Job Title.
Is the best way to implement this to create the Org_has_users class and use @OneToMany and @ManyToOne annotations?
Would cascade on save ensure that I can access the full join relationships from both the Users and Organisations classes?
I hope this is enough to get started. I am more interested in how you would implement this.
Thanks!
Anthony
1
The easiest way to implement this is to place an entity between the two Entities, which holds the additional values.
There are downsides to this approach: You need to reference the table from one or two sides, what makes it harder to maintain the correct values and adds in the worst case additional linking tables – all values must be removed in both sides of the collection and all JoinTables must be correct.
I don’t know exactly about eBeans, but there are ways to do this, see here (especially the comment to the answer):
https://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns
1