Thursday, October 8, 2009

Update on JPA 2.0 and Apache OpenJPA

Developerworks just posted an article from Kevin Sutter, our lead for Java persistence, on JPA 2.0 and the work at Apache on OpenJPA. Click here to learn about the new features that are coming in JPA 2.0 and get an early preview via early access drivers at Apache here.

3 comments:

Neal Ravindran said...

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

Kevin Sutter said...

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

Neal Ravindran said...

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