[英]Does @ElementCollection imply orphanRemoval?
According to this post Difference between @OneToMany and @ElementCollection?根据这篇文章@OneToMany 和 @ElementCollection 之间的区别? I should prefer @ElementCollection
for embeddable types and @OneToMany
for entities.我应该更喜欢@ElementCollection
用于可嵌入类型,而@OneToMany
用于实体。 But using @OneToMany
I can additionaly set option orphanRemoval=true
.但是使用@OneToMany
我可以额外设置选项orphanRemoval=true
。 How can I do this with @ElementCollection
?我怎样才能用@ElementCollection
做到这一点? It it implied?它暗示了?
It is implied.它是隐含的。 Removing the owning entity would also remove all data on the @ElementCollection
.删除拥有实体也会删除@ElementCollection
上的所有数据。 Setting the Collection
to null or changing elements in the Collection
would cause an update if Session
isn't already closed.设置Collection
为空或变化的元素Collection
,如果会导致更新Session
尚未关闭。
The official documentation here says this: 这里的官方文档是这样说的:
2.8.1. 2.8.1. Collections as a value type作为值类型的集合
Value and embeddable type collections have a similar behavior as simple value types because they are automatically persisted when referenced by a persistent object and automatically deleted when unreferenced.值和可嵌入类型集合具有与简单值类型相似的行为,因为它们在被持久对象引用时自动持久化,而在未引用时自动删除。 If a collection is passed from one persistent object to another, its elements might be moved from one table to another.如果一个集合从一个持久对象传递到另一个,它的元素可能会从一个表移动到另一个表。
... ...
For collections of value types, JPA 2.0 defines the @ElementCollection annotation.对于值类型的集合,JPA 2.0 定义了 @ElementCollection 注释。 The lifecycle of the value-type collection is entirely controlled by its owning entity.值类型集合的生命周期完全由其拥有实体控制。
I ran these three tests to test it out:我运行了这三个测试来测试它:
@Test
public void selectStudentAndSetBooksCollectionToNull() {
Student student = studentDao.getById(3L);
List<String> books = student.getBooks();
books.forEach(System.out::println);
student.setBooks(null);
em.flush(); // delete from student_book where student_id = ?
}
@Test
public void selectStudentAndAddBookInCollection() {
Student student = studentDao.getById(3L);
List<String> books = student.getBooks();
books.add("PHP Book");
books.forEach(System.out::println);
em.flush(); // insert into student_book(student_id, book) values(?, ?)
}
@Test
public void selectStudentAndChangeCollection() {
Student student = studentDao.getById(3L);
List<String> newBooks = new ArrayList<>();
newBooks.add("Rocket Engineering");
newBooks.forEach(System.out::println);
student.setBooks(newBooks);
em.flush(); // delete from student_book where student_id = ?
// insert into student_book(student_id, book) values(?, ?)
}
This is the Student
class:这是Student
类:
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_id", nullable = false, insertable = false, updatable = false)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@ElementCollection
@CollectionTable(
name = "student_books",
joinColumns = @JoinColumn(name = "student_id", referencedColumnName = "student_id"))
@Column(name = "book")
private List<String> books = new ArrayList<>();
// Getters & Setters
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.