Fetch join causes duplicate entity (proxy + initialized) in collection

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:

  1. I load A using:
@Query("FROM A WHERE id = :id")

  1. I load B using 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?

If the persistence context already contains a proxy object for an entity with a primary key 1, then every direct or indirect fetch of an entity of that type shall return the proxy.
The duplication of the objects in the set does seem wrong to me.

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.