使用 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中文网其他相关文章!