【问题标题】:Prevent hibernate remove @Transient child objects automatically when parent is saved [duplicate]保存父对象时防止休眠自动删除@Transient子对象[重复]
【发布时间】: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


【解决方案1】:

这个问题与 DataJpaTest 相关,也许,在保存数据后它会刷新所有字段,如果不是,则 Persistent 会分配一个空值。你可以用一个真实的容器试试,我认为它可以正常工作。

【讨论】:

  • 没有。我首先遇到了真实容器的问题,然后我创建了这个测试来证明它。
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 2013-05-25
  • 2019-11-21
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
相关资源
最近更新 更多