What are the JPA entity relationship annotations (@OneToMany, @ManyToOne, @ManyToMany)? JPA एंटिटी रिलेशनशिप एनोटेशन (@OneToMany, @ManyToOne, @ManyToMany) क्या हैं?
JPA relationship annotations map object references between entities to foreign keys or join tables. @OneToOne maps a one-to-one association, @OneToMany/@ManyToOne map a one-to-many relationship from both sides (e.g. one Author has many Books, each Book has one Author), and @ManyToMany maps a many-to-many relationship, typically backed by a join table.
Each supports mappedBy to designate the owning side, cascade to propagate operations like persist or delete to related entities, and fetch (LAZY or EAGER) to control when related data is loaded from the database.
@Entity
public class Author {
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Book> books;
}
@Entity
public class Book {
@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
}JPA रिलेशनशिप एनोटेशन एंटिटीज़ के बीच ऑब्जेक्ट संदर्भों को फॉरेन की या जॉइन टेबल में मैप करते हैं। @OneToOne एक-से-एक संबंध मैप करता है, @OneToMany/@ManyToOne एक-से-अनेक संबंध को दोनों ओर से मैप करता है, और @ManyToMany अनेक-से-अनेक संबंध मैप करता है, जो आमतौर पर जॉइन टेबल पर आधारित होता है।
हर एनोटेशन mappedBy (ओनिंग साइड बताने के लिए), cascade (persist/delete जैसे ऑपरेशन्स को संबंधित एंटिटीज़ तक फैलाने के लिए), और fetch (डेटा कब लोड हो, यह नियंत्रित करने के लिए) सपोर्ट करता है।
@Entity
public class Author {
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Book> books;
}
@Entity
public class Book {
@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
}Was this answer clear?