2

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:

An ERD

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

2

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

  • Hi Volker,Thanks for the link. I agree it seems the easier way is to place an entity between the two. It looks like it may also be better to not Cascade and save manually in order to ensure the integrity of the joins. I shall have a play! Thanks again – afxdesign Aug 2 '12 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.