Many-to-Many, @JoinTable, Bidirectional vs Unidirectional
Learn Many-to-Many, @JoinTable, Bidirectional vs Unidirectional (Phase 4 · Entity Relationships (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
@ManyToMany with @JoinTable • @ManyToMany models students ↔ courses with a join (link) table holding two foreign keys. The owning side declares @JoinTable(name, joinColumns, inverseJoinColumns) ; the other side uses mappedBy . • Use a Set (not List ) for many-to-many collections: with a List (a "bag") Hibernate deletes all link rows and re-inserts them on any change. Never use CascadeType.REMOVE/ALL on many-to-many (deleting one entity would delete shared ones). • Keep both sides in sync with helper methods and implement equals/hashCode on a business key so Set semantics work.