Hibernate Many to Many Relationship
@Entity
public class Employee { // ... Set<Project> projects = new HashSet<>(); // standard constructor/getters/setters }@JoinTable specifies that an intermediate table is used to map the ManyToMany relationship it specifies the Join column as the id of the Employee class from which the Many side of the relationship is being created. The inverseJoinColumns attribute specifies the Join column of the target of the M2M relationship.
public class Project {
// ...
private Set<Employee> employees = new HashSet<>();
// standard constructors/getters/setters
}
in the target entity of the M2M relationship we specify a @ManyToMany annotation using mappedBy attribute.
As we can see, both the Employee class and Project classes refer to one another, which means that the association between them is bidirectional.
Comments
Post a Comment