One-to-One, Many-to-One, One-to-Many, @JoinColumn and mappedBy
Learn One-to-One, Many-to-One, One-to-Many, @JoinColumn and mappedBy (Phase 4 · Entity Relationships (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
@ManyToOne: the foundation • @ManyToOne (many enrolments → one course) puts the foreign key column in the table of the "many" side : it is the owning side and the simplest, most efficient mapping. Use @JoinColumn(name = "course_id", nullable = false) . • Default fetch is EAGER for @ManyToOne / @OneToOne : set fetch = FetchType.LAZY explicitly almost always, to avoid loading extra rows and N+1 queries. • Owning side = the side that has the foreign key and whose changes Hibernate persists; inverse side = the side with mappedBy (read-only mirror).