False positives for transient-checks for deleted entities

The new transient-checks for deleted entities introduced with 6.6.0 are pretty useful because they surfaces implementation glitches. However, it raises false errors when entities are remove with a bulk deletion. For example, deleting entities with

em.createQuery("DELETE FROM Deployment d WHERE d.m_scope =: scopeId")
    .setParameter(":scopeId", scopeId)
    .executeUpdate();

will raise a wrong TransientObjectException later on that claims that one of the deleted Deployments is referencing another deleted (and hence transient) entity. However, using

em.createQuery("SELECT d FROM Deployment d WHERE d.m_scope = :scopeId")
    .setParameter(":scopeId", scopeId)
    .getResultStream()
    .forEach(em::remove);

i.e. an explicit remove works as expected without raising an exception.

The thing is, you have to detach objects from the session before you run the bulk deletion queries and remove all references to the objects that are about to be deleted from entities within the session. Also see the word of caution in the specification.

Makes sense. But it can become pretty inefficient if there is a large number of entities. You have to get all of them from the DB, load them into memory just to immediately remove them right afterwards because you don’t know which one still references the other entity.

If you have bi-directional associations, you can specify the CascadeType.DETACH for cascade member of @OneToOne/@OneToMany/@ManyToMany, which will make this “automatic”.
I don’t have a better answer.