A many-to-many-relationship is realized by the JPA annotation @ManyToMany.
On both sides there is a collection marked with that annotation. One of this side must be the owner / manager of the relationship - mappedBy:
@Entity
public class A {
@Id
private Long id;
...
@ManyToMany
private List<B> colBs;
}
@Entity
public class B {
@Id
private Long id;
...
@ManyToMany(mappedBy="colBs")
List<A> colAs;
}
So entity b is owner of the n-m-relationship.
A n-m-relationship is always stored in a distinct table:
The name is made of:
entity name of the owner of the relationship + _ + entity name of the other side.
In this case: A_B.
The foreign key column for generating the relation is per default made by this pattern:
Attribute name of the referred side + _ + primary key name.
In this example: colbs_id respectively colas_id
No comments:
Post a Comment