Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
    
This maybe relevant: biese.wordpress.com/2008/10/08/… –  Emmad Kareem Aug 2 '12 at 10:12

1 Answer 1

up vote 2 down vote accepted

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): http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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