假設我有一對多的關系:

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;
@Entity
@Table(name = "books")
public class Book implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
@Column(unique = true)
private String isbn;
@OneToMany(mappedBy = "book", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
private Set<Page> pages;
public Book() {
}
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
// getters and setters, equals(), toString() .... (omitted for brevity)
}
和
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "pages")
public class Page implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int number;
private String content;
private String chapter;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "book_id", nullable = false)
private Book book;
public Page() {
}
public Page(int number, String content, String chapter, Book book) {
this.number = number;
this.content = content;
this.chapter = chapter;
this.book = book;
}
// getters and setters, equals(), toString() .... (omitted for brevity)
}
并將其存盤在:
// create a new book
Book book = new Book("Java 101", "John Doe", "123456");
// save the book
bookRepository.save(book);
// create and save new pages
pageRepository.save(new Page(1, "Introduction contents", "Introduction", book));
pageRepository.save(new Page(65, "Java 8 contents", "Java 8", book));
pageRepository.save(new Page(95, "Concurrency contents", "Concurrency", book));
問題是:我真的需要save手動訪問每個頁面嗎?我可以只使用 java 集合,然后只保存主要物體,如:
book.pages.add(new Page(1, "Introduction contents", "Introduction", book));
book.pages.add(new Page(65, "Java 8 contents", "Java 8", book));
book.pages.add(new Page(95, "Concurrency contents", "Concurrency", book));
bookRepository.save(book);
該示例取自https://attacomsian.com/blog/spring-data-jpa-one-to-many-mapping
而且我想知道存盤關系的正確和最佳方式,這就是為什么我在這里問而不只是自己進行實驗,因為這篇文章似乎誤導了很多人
uj5u.com熱心網友回復:
下面是cascade在Book#pages發揮作用。正如您將其配置為那樣CascadeType.ALL,這意味著當您使用EntityManagerto persist()on a 時Book,它將自動呼叫persist()其所有Page.
使用cascadeis disable ,您必須呼叫以下命令來保存新 Book 及其所有頁面:
entityManager.persist(book);
entityManager.persist(page1);
entityManager.persist(page2);
....
entityManager.persist(pageN);
但是在啟用級聯的情況下,您只需要呼叫以下內容,因為persist()頁面上的所有內容都將被級聯以自動呼叫。
entityManager.persist(book);
由于 spring-data-jpa 存盤庫內部僅使用相同的EntityManager方式保存同一事務的物件,因此如果啟用級聯,您只需要呼叫以下代碼即可保存書籍及其頁面:
Book book = new Book("Java 101", "John Doe", "123456");
book.pages.add(new Page(1, "Introduction contents", "Introduction", book));
book.pages.add(new Page(65, "Java 8 contents", "Java 8", book));
book.pages.add(new Page(95, "Concurrency contents", "Concurrency", book));
bookRepository.save(book);
否則,如果級聯被禁用,您必須呼叫以下代碼來保存 Book 及其頁面:
Book book = new Book("Java 101", "John Doe", "123456");
bookRepository.save(book);
pageRepository.save(new Page(1, "Introduction contents", "Introduction", book));
pageRepository.save(new Page(65, "Java 8 contents", "Java 8", book));
pageRepository.save(new Page(95, "Concurrency contents", "Concurrency", book));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399235.html
下一篇:DocumentApp的TableCell.appendImage在影像之前創建了一個不必要的段落。我怎樣才能避免這種情況?
