Farewell to IBM
10 years ago
Thoughts and opinions on products across the WebSphere brand with a wide range of contributers across multiple products, including WebSphere Application Server, WebSphere Portal Server, WebSphere Process Server and more, as well as across multiple specialties, including technical architects, marketing, management, and user experience. The postings on this site are our own and don’t necessarily represent IBM’s positions, strategies or opinions.
Subscribe to: RSS Feed
3 comments:
I wish they did a simple addition to JPA when it comes to manyTomany mapping where the join table has extra attributes. For example, if I have a student_course join table and have an extra attribute(like say start date and end date of course), how do I map Student and course using @ManytoMany.
[current workaround is to treat as two oneToMany relations between join-table and the student and course tables]
Extra attributes in join table causes headaches in mapping
Or am I wrong...is there a soln other than the workaround I mentioned above?
~Neal
Hi Neal,
Storing relationship state is a bit more difficult in the Java object world (as compared to the Database relational world). Just providing the simple ManyToMany relationship between Student and Course is relatively straight forward. But, when you throw in the extra attribute state (start and end dates), then it gets more difficult.
The most common solution is to turn the relationship into an Entity. This new Entity would provide both the desired state attributes as well as the relationship data (replacing the standard join table). This new Entity will have a ManyToOne relationship to the existing Entity types. And, each of these existing Entity types would have a OneToMany relationship with the new Entity type.
The primary key of this new Entity (relationship) would be a combination of the keys for the existing Entity types.
A good example of this can be found in Chapter 8 of the Pro EJB 3, Java Persistence API book by Mike Keith and Merrick Schincariol.
Hope this helps,
Kevin
Thanks for the reply, Kevin.
What you and I have suggested are both more unnecessary work and that is precisely my point...why do we have an @ManyToMany annotation and then go this long winded route? Why isn't this out-of-the-box when someone uses the annotation. This looks like a simple thing to add instead of keeping on adding other bigger and not much used features to JPA.
All that said..was never aware of that example in the EJB text book..Thanks for pointing it out
Post a Comment