I'm developing a logic in the server side of my project that will update an Entity in the database. But this entity has reference to a list of another entity.
So, for example I have an Entity Test
like this one:
@Entity
public class Test {
@Id
@Column(name = "idTest", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "idTest", targetEntity = Test2.class, fetch = FetchType.LAZY)
private Collection<Test2> test2Collection;
}
which has references to Test2
Entity.
@Entity
public class Test2 {
@Id
@Column(name = "idTest2", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String field1;
private String field2;
@ManyToOne
@JoinColumn(name = "idTest", referencedColumnName = "idTest")
private Test idTest;
}
In my case, I'm updating the Collection test2Collection
of Test
entry and eventually I'm removing some of Test2
entries of it.
The simple merge
method from EntityManager
does not detect that some entries has been deleted when I call it passing the new updated Test
entry.
I was thinking to track the removed entries from test2Collection
by making a diff between the Test
entry before updating the database with the new Test
entry to be updated.
But I'm not sure this is the only (and best) way of doing this. Anyone else has another idea?