使用 Spring Boot 和 JPA(Java Persistence API)建立應用程式時,管理實體之間的關係至關重要。了解如何有效地映射這些關係將幫助您準確地建模資料並執行有效的查詢。在本指南中,我們將探討 Spring Boot 支援的不同類型的 JPA 映射:一對一、一對多、多對一和多對多。將提供有關單向和雙向映射的範例。
在單向一對一關係中,一個實體引用另一個實體,而第二個實體不知道第一個實體。
範例:
// User.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; @Data @Entity public class User { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String username; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "fk_profile_id", referencedColumnName = "id") private Profile profile; } // Profile.java import javax.persistence.Entity; import javax.persistence.Id; @Data @Entity public class Profile { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String bio; }
在雙向一對一關係中,兩個實體互相引用。
範例:
// User.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; @Data @Entity public class User { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String username; @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) private Profile profile; } // Profile.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.JoinColumn; @Data @Entity public class Profile { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String bio; @OneToOne @JoinColumn(name = "user_id") private User user; }
在單向一對多關係中,一個實體引用另一個實體的集合。
範例:
// Author.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import java.util.List; @Entity @Data public class Author { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "fk_book_id", referencedColumnName = "id") private List<Book> books; } // Book.java import javax.persistence.Entity; import javax.persistence.Id; @Entity @Data public class Book { @Id private Long id; private String title; }
在雙向一對多關係中,兩個實體互相引用。
範例:
// Author.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.CascadeType; import javax.persistence.FetchType; import java.util.List; @Entity @Data public class Author { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List<Book> books; } // Book.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity @Data public class Book { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String title; @ManyToOne(name = "fk_author_id", referencedColumnName = "id") private Author author; }
在單向多對一關係中,多個實體引用單一實體。
範例:
// Order.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity @Data public class Order { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String description; @ManyToOne private Customer customer; } // Customer.java import javax.persistence.Entity; import javax.persistence.Id; @Entity @Data public class Customer { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; }
在雙向多對一關係中,被引用實體具有引用實體的集合。
範例:
// Order.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity @Data public class Order { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String description; @ManyToOne private Customer customer; } // Customer.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import java.util.List; @Entity @Data public class Customer { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; @OneToMany(mappedBy = "customer") private List<Order> orders; }
在單向多對多關係中,每個實體都有另一個實體的集合,但該關係不由任何一方明確管理。
範例:
// Student.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import java.util.Set; @Entity @Data public class Student { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; @ManyToMany private Set<Course> courses; } // Course.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import java.util.Set; @Entity @Data public class Course { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String title; @ManyToMany private Set<Student> students; }
在雙向多對多關係中,兩個實體相互引用,並且關係是明確管理的。
範例:
// Student.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import java.util.Set; @Entity @Data public class Student { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String name; @ManyToMany(mappedBy = "students") private Set<Course> courses; } // Course.java import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import java.util.Set; @Entity @Data public class Course { @Id @GeneratedValue(stratergy = GenerationType.Identity) private Long id; private String title; @ManyToMany private Set<Student> students; }
以上是了解 Spring Boot 中的 JPA 映射:一對一、一對多、多對一和多對多關係的詳細內容。更多資訊請關注PHP中文網其他相關文章!