【发布时间】:2021-05-22 01:48:45
【问题描述】:
在 Spring JPA 中,我将一个子列表声明为 @Transient,我不想与父级保持一致。问题是当我保存它的父级时,子级列表被休眠删除了。例如:
@RunWith(SpringRunner.class)
@DataJpaTest
public class JPAUnitTest {
@Autowired
TutorialRepository repository;
@Test
public void paymentsShouldExistAfterSaved() {
Tutorial tutorial = new Tutorial("Tut title", "Tut desc", true);
tutorial.addStudent(new Student("John"));
long id = repository.save(tutorial).getId();
Tutorial found = repository.findById(id).get();
org.junit.Assert.assertEquals("John", found.getStudents().get(0).getName());
found.getStudents().clear();
Student s = new Student("Kelly");
s.addPayment(100, LocalDate.of(2020, 11, 9));
s.addPayment(550, LocalDate.of(2020, 12, 18));
s.addPayment(80, LocalDate.of(2020, 12, 29));
found.addStudent(s);
org.junit.Assert.assertEquals(3, found.getStudents().get(0).getPayments().size());
repository.save(found);
org.junit.Assert.assertEquals("Kelly", found.getStudents().get(0).getName());
//Error - expected:3 but was:0
org.junit.Assert.assertEquals(3, found.getStudents().get(0).getPayments().size());
}
在上面的测试中,最后一个断言失败,因为在教程持续后付款消失了。付款是学生类的孩子:
@Entity
@Table(name = "payments")
public class Payment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "amount")
private double amount;
@Column(name = "payment_date")
private LocalDate date;
public Payment() {}
public Payment(double amount, LocalDate date) {
this.amount = amount;
this.date = date;
}
}
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.EAGER)
private Tutorial tutorial;
@Transient
private List<Payment> payments = new ArrayList<>();
public Student() {}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Payment> getPayments() {
return payments;
}
public void addPayment(double amount, LocalDate date) {
this.payments.add(new Payment(amount, date));
}
}
@Entity
@Table(name = "tutorials")
public class Tutorial {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "published")
private boolean published;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "tutorial")
private List<Student> students = new ArrayList<>();
public Tutorial() {}
public Tutorial(String title, String description, boolean published) {
this.title = title;
this.description = description;
this.published = published;
}
public void addStudent(Student student) {
this.students.add(student);
}
public List<Student> getStudents() {
return students;
}
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isPublished() {
return published;
}
public void setPublished(boolean isPublished) {
this.published = isPublished;
}
}
我的版本:
Spring Boot 2.2
Java 1.8
【问题讨论】:
-
@Gregg 如果可以,请详细说明答案。我了解@Transient 值如何在数据库端丢失。 java 侧字段是否在其持久化之前被删除?为什么它会影响 java 中的
found对象? save() 返回持久化的值,但它看起来不像被提取到变量中。
标签: java hibernate spring-data-jpa hibernate-mapping