首頁 > Java > java教程 > 主體

了解 Spring Boot 中的 JPA 映射:一對一、一對多、多對一和多對多關係

WBOY
發布: 2024-08-13 16:39:56
原創
657 人瀏覽過

Understanding JPA Mappings in Spring Boot: One-to-One, One-to-Many, Many-to-One, and Many-to-Many Relationships

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

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!