My code is involving 3 entities:
Code:
@Entity
public class Entity1 implements Serializable {
...
@OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER, targetEntity = Entity3.class,mappedBy = "entity1")
private Collection<Entity3> entity3;
@OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER, targetEntity = Entity2.class,mappedBy = "entity1")
private Collection<Entity2> entity2;
}
@Entity
public class Entity2 implements Serializable {
...
@EmbeddedId
private Entity2IdPK id;
@ManyToOne(optional=true,fetch = FetchType.EAGER,targetEntity = Entity1.class)
private Entity1 entity1;
}
@Entity
public class Entity3 implements Serializable {
...
@EmbeddedId
private Entity3IdPK id;
@MapsId("entity1")@ManyToOne(optional=true,targetEntity = Entity1.class)
private Entity1 entity1;
I'm performing a search using a Spring repository in order to get one instance of Entity1. I know that the element I'm querying from the database has "1 Entity2" elements, and "3 Entity3" element in the database.
When I loop over the collection Entity1.entity3, I find 3 elements, which is normal.
But when I loop over the collection Entity1.entity2, I find 3 elements, which are pointing to the same object, whereas I was expecting only 1 element.
Same result if I add more Entity3 elements, the 2 collections have the same number of elements. This is very strange, because when I try to replicate the same environment, with the same database content, and the same code, on another computer, I have the expected (1 element of Entity2 in the Collection).
Is it a normal behaviour ? Is it a bug in my code, or is it a bug in Hibernate ?