gpt4 book ai didi

java - 防止 hibernate 在保存父对象时自动删除@Transient 子对象

转载 作者:行者123 更新时间:2023-12-05 06:51:05 26 4
gpt4 key购买 nike

<分区>

在 Spring JPA 中,我将子列表声明为 @Transient,我不想与父列表保持一致。问题是当我保存它的 parent 时, child 的列表被 hibernate 删除了。例如:

@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());

}

在上面的测试中,最后一个断言失败了,因为支付在教程持续存在后就消失了。付款是学生类(class)的 child :

@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 启动 2.2
Java 1.8

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com