Hi everyone, I’m facing a strange behavior when dealing with fetch join. I end up with two instances of the same entity in a collection — one is a fully fetched entity and the other is a HibernateProxy.
Here is the setup:
I have an abstract superclass:
@Entity
public abstract class Unit {
@Id
private UUID id;
}
And two classes that reference it:
@Entity
public class A {
@ManyToOne(fetch = FetchType.LAZY)
private ModuleUnit moduleUnit;
}
@Entity
public class B {
@OneToMany(fetch = FetchType.LAZY)
private Set<Unit> units = new LinkedHashSet<>();
}
Now what I’m doing:
- I load
Ausing:
@Query("FROM A WHERE id = :id")
- I load
Busing BlazePersistence + QueryDSL:
queryFactory.select(b)
.from(b)
.leftJoin(b.units, unit).fetchJoin()
.fetch();
Result: in the collection of B I see duplicate Unit entries.
One is the fetched entity, the other is a HibernateProxy.
I figured out that this happens because A has a ManyToOne reference toUnit. If that reference was not fetched in query (1), then query (2) produces both a proxy and a fully initialized instance of the same entity.
My question: Is this the expected behavior?