
- Spring Boot ORM 教程
- Spring Boot ORM - 首頁
- Spring Boot ORM - 概述
- 環境設定
- Spring Boot ORM - JPA
- Spring Boot ORM & Spring Data JPA
- Spring Boot ORM - 建立專案
- Application.properties
- Spring Boot ORM - 更新專案
- Spring Boot ORM - 測試Hibernate
- Spring Boot ORM & EclipseLink
- Maven EclipseLink
- 更新專案 EclipseLink
- Spring Boot ORM - 測試 EclipseLink
- Spring Boot ORM 有用資源
- Spring Boot ORM - 快速指南
- Spring Boot ORM - 有用資源
- Spring Boot ORM - 討論
Spring Boot ORM - JPA
Java 永續性 API (JPA) 是一組類和介面,允許將 Java 物件與資料庫進行物件/關係對映。在 JPA 中,實體類在persistence.xml檔案中定義。Spring Boot 不使用persistence.xml。相反,它使用“實體掃描”,這是一個查詢並將實體類註冊到永續性提供程式的過程。
什麼是實體掃描?
實體發現 - 當提供程式找到帶有@Entity註釋的類時,它會將其識別為永續性實體並將其註冊。
持久單元 - 然後將發現的實體與持久單元關聯,持久單元定義由單個EntityManager管理的一組實體。
掃描包 - JPA 提供程式掃描指定的包或目錄,查詢用@Entity註釋的類。
預設情況下會掃描自動配置的包。
在 Spring Boot 中配置實體掃描
預設掃描 - 預設情況下,Spring Boot 掃描主應用程式類的包及其子包中的實體類。
自定義掃描 - 你可以使用 @EntityScan 註解來指定要掃描的包。例如
@SpringBootApplication @EntityScan(basePackages = {"com.example.entities"}) public class Application { // ... }
Spring Data JPA 倉庫
Spring Data JPA 實現用於訪問資料庫(關係型、NoSQL 或其他任何型別)資料的介面。Spring Data 倉庫擴充套件自Repository或CrudRepository。
@Repository public interface EmployeeRepository extends CrudRepository<Employee, Integer> { }
建立和刪除資料庫
如果你使用嵌入式資料庫(如 H2、Derby),JPA 資料庫會自動建立。要建立和刪除表,你的 application.properties 應該如下所示:
spring.jpa.hibernate.ddl-auto=create-drop
對映類之間的關聯
關聯對映可以分為兩種型別:
單向關聯 - 在這種關聯中,只有一個實體持有對另一個實體的引用。例如,一對多或多對一關係。
雙向關聯 - 在雙向關聯中,兩個實體(源和目標)都具有一個相互引用的關係欄位。例如,多對多關係。
一對多關係(單向)
想象一下部門實體及其員工。每個部門都有許多員工,但每個員工只屬於一個部門。
Department.java
package com.tutorialspoint; import javax.persistence.*; import java.util.*; @Entity public class Department { @Id private Long id; @OneToMany @JoinColumn(name = "department_id") private List<Employee> employees; public Department() {} public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
Employee.java
package com.tutorialspoint; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Employee { @Id private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
多對一關係(單向)
考慮一下學生和學校。每個學生只能在一個學校註冊,但每個學校可以有多個學生。
Student.java
package com.tutorialspoint; import javax.persistence.*; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne @JoinColumn(name = "school_id") private School school; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } }
School.java
package com.tutorialspoint; import javax.persistence.Entity; import javax.persistence.Id; import java.util.List; @Entity public class School { @Id private Long id; private List<Student> students; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
多對多關係(雙向)
在雙向關聯中,兩個實體(源和目標)都具有一個相互引用的關係欄位。想象一下書籍和作者。每本書都有一個作者,但每個作者可以寫多本書。
Book.java
package com.tutorialspoint; import javax.persistence.*; @Entity public class Book { @Id private Long id; private String title; @ManyToOne @JoinColumn(name = "author_id") private Author author; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } }
Author.java
package com.tutorialspoint; import javax.persistence.*; import java.util.List; @Entity public class Author { @Id private Long id; private String name; @OneToMany(mappedBy = "author") private List<Book> books; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } }