RemoteException: Cannot merge an Entity that has been removed


I’m trying to make a method inside a bean, that get’s called periodically to clean the DB from the soft-deleted entities older than a day, let’s say.

        Collection<MetaClass> metaClasses = metadata.getClasses();

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -1);

    CommitContext commitContext = new CommitContext();

    metaClasses.forEach(metaClass -> {
        List<Entity<Object>> list = transactionalDataManager.load(metaClass.getJavaClass())
                        "select e from " + metaClass.getName() + " e " +
                                "where e.deleteTs is not null" +
                                "and e.deleteTs < :date"
                .parameter("date", calendar.getTime())


    EntitySet entitySet = dataManager.commit(commitContext);"Successfully deleted " + entitySet.size() + " entities .");

This is the method that does all the magic, but at the dataManager.commit(commitContext) line, I get the following exception:
remove-issue.txt (10.8 KB)

Why can this happen?

Thank you,


Hi Alex,

It looks like the same object appears in the commit context twice.
Try to debug or print the collected entities before dataManager.commit().


Hello Konstantin,

How is it possible to find duplicates in a HashSet?

This is hardly possible, I agree.
But I can imagine a situation when you have an eager fetching and cascade delete on the JPA level, which could lead to the error.

I’m pretty sure this is the case. Do you have a suggestion for how can I deal with such a case?

We don’t recommend using JPA eager fetching and cascade operations, it is explicitly stated in the docs.

I misunderstood your first statement, we do NOT use JPA cascade annotations anymore, we use only the @OnDelete and @OnDeleteInverse annotations provided by the platform.

I cannot reproduce the error using a relationship with the normal @OnDelete instruction. Also, the stack trace shows that the error occurs long before the cascade deletion mechanism.
Can you attach the entity class and all entities it is connected to?