Linwood Hayes wrote:Isn't this a many-to-many bidirectional mapping ? And it does not use "mappedBy". Please share your thought.
Roel De Nijs wrote:
Linwood Hayes wrote:Isn't this a many-to-many bidirectional mapping ? And it does not use "mappedBy". Please share your thought.
No, it isn't. If you look at the javadoc of the mappedBy attribute of the @ManyToMany annotation, you'll notice that this attribute is required if you want a bidirectional relationship.
Frits Walraven wrote:When you don't have a mappedBy attribute JPA will create two unidirectional many-to-many relationships, having two different join tables. You can see that if you remove the @JoinTable annotations from your example and let JPA create the default mapping.
I don't get it. Table design was done prior to any java code and it was designed to have only one link table. what do you mean it will create two different join tables ?
2.9 Entity Relationships
The following rules apply to bidirectional relationships:
- The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship.
... left out the other rules
Frits Walraven wrote:
I don't get it. Table design was done prior to any java code and it was designed to have only one link table. what do you mean it will create two different join tables ?
Then the mapping to that existing table is not conform how a bidrectional many-to-many relationship should be mapped according to the JPA specifications.
The specification clearly states that the mappedBy attribute is needed for a bi-directional relationship.
2.9 Entity Relationships
The following rules apply to bidirectional relationships:
- The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship.
... left out the other rules
I was just telling you that you don't see your mistake because you point from both unidirectional many-to-many sides to the same join table. Leaving out the mappedBy attribute by a many-to-many relationship creates an unidirectional relationship with a join-table by default (and in this case two).
Linwood Hayes wrote:But can you tell me what would exactly happen if I use my implementation ?
Linwood Hayes wrote: But can you tell me what would exactly happen if I use my implementation ?