A ManyToMany
mapping describes a relationship between to entities where both can be related to more than one instance of each other, and is defined by the @ManyToMany
annotation.
Unlike @OneToMany
where a foreign key column in the table of the entity can be used, ManyToMany
requires a join table, which maps the entities to each other.
Annotation | Purpose |
---|---|
@TableGenerator | Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation |
@GeneratedValue | Provides for the specification of generation strategies for the values of primary keys. It may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. |
@ManyToMany | Specifies relationship between Employee and Project entities such that many employees can work on multiple projects. |
mappedBy="projects" | Defines a bidirectional relationship between Employee and Project |
@JoinColumn | Specifies the name of column that will refer to the Entity to be considered as owner of the association |
@JoinTable | Specifies the table in database which will hold employee to project relationships using foreign keys |
Full example can be referred here