In the context of a Spring Boot project using Spring Data JPA, I have defined the following entities:
- Ent1 contains a list of Ent2 elements
- Ent2 contains a list of Ent3 elements
When fetching a top-level Ent1 object through a repository, I'm seeing that every Ent2 which has more than one child appears multiple times in the Ent1.ent2 list. For example, an Ent2 with two childs will appear twice.
So instead of getting this:
I'm getting this:
Notes:
- There are no duplicates in the database
- If I delete
ent3bin the database, the duplicatedent2disappears
Here's a simplified version of the code:
```java
@Entity
public class Ent1 {
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Ent2> ent2 = new ArrayList<Ent2>();
}
@Entity
public class Ent2 {
@ManyToOne
@JoinColumn(name = "PARENT_ID", nullable = false)
protected Ent1 parent;
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Ent3> ent3 = new ArrayList<Ent3>();
}
@Entity
public class Ent3 {
@ManyToOne
@JoinColumn(name = "PARENT_ID", nullable = false)
protected Ent2 parent;
}
```

