Prevent hibernate remove @Transient child objects automatically when parent is saved

Viewed 301

In Spring JPA, I declared a childs list as @Transient which I don't want to persist with parent. Problem is when I saved its parent, the childs list was removed by hibernate. For example:

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

  }  
  

In the test above, the last assertion failed because the payments are gone after tutorial persisted. The payments are children of Student class:

@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;
    }
}

My version:
Spring Boot 2.2
Java 1.8

1 Answers

This problem related to DataJpaTest, maybe, after saving data it refreshes all fields if it Persistent if not so assigns a null value. You can try it with a real container I think it will work normally.

Related