Talk:Java Persistence/ManyToMany

Example join table association object annotations
In the example, after this line:

shouldn't there be something like

or is this done automatically by the persistence handler?


 * Correct, this was missing in the example code. I updated it.  JPA does not maintain bi-directional relationships, it is the object model's responsibility.
 * James (talk) 14:38, 13 April 2010 (UTC)


 * Thanks for the quick response. There's some really helpful stuff in this wikibook, so I'm glad I could contribute at least a tiny bit.

Uhm, does this example work for you? I'll get an exception, which is - as far as I understand - caused by the fact that the association class also needs to be persisted. This could be resolved by marking the one-to-may relations as cascading: respectively

My problem then is, however, that the foreign keys in the project association class are not set and consequently the database throws an "NOT NULL" exception. This means basically, that I have to create and persist the employees and projects inf a first transaction, and create the assicoation between them in a second transaction. Or is there any better way?


 * Sorry this last paragraph was wrong. I messed up my annotations, that was why the relation was not properly handled by the entity manager. In other words, creating and persisting a project association object works perfectly, if cascading is enabled (see above). I'll leave the paragraph anyway, maybe this can help someone with the same problem.

Please rename the column names
It is not clear what column of what table is used in annotation: Is it EMPLOYEE.EMP_ID to EMP_PROJ.EMP_ID or vice versa?


 * Fixed in the example. For a JoinTable the JoinColumn is from the perspective of the JoinTable, the name is the column in the JoinTable, the referencedColumnName is the column in the source/target table.  referencedColumnName is optional unless mapping to a composite Id.
 * James (discuss • contribs) 17:40, 30 July 2012 (UTC)