Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Pre-query flush seems not to flush all deletions in the correct order

Hello,
I have the following problem that puzzles me.

I have an entity, let's call it First, which references another entity, let's call it Second.

The relationship is this:

@Entity
public class First {
  // ...
  @OneToOne(
            cascade = { CascadeType.DETACH, CascadeType.PERSIST,
                    CascadeType.REFRESH },
            fetch = FetchType.LAZY,
            optional = false,
            orphanRemoval = false)
    private Second second;
}

The relationship is unidirectional, i.e. there's no @OneToOne relationship mapped in Second towards First.

First also has a @ManyToOne relationship towards a FirstCollection entity. I don't think it should matter at all, but I say it for completeness.

Then I have some code that takes a First instance (let's call it "first") and does some things like:

FirstCollection firstCollection = first.getFirstCollection();
// do some read-only operations on first
// remove first from the FirstCollection related entities
firstCollection.getFirsts().remove(first);
// delete first
entityManager.remove(first);
// delete the related second instance
entityManager.remove(first.getSecond());
// perform a read query on an entity related to first

When the last step is executed, a flush before the execution of the read query is performed; I see something like this in the stack trace:
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:879)
	at org.eclipse.persistence.internal.jpa.QueryImpl.performPreQueryFlush(QueryImpl.java:967)
	at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:207)

The problem is that this lfush seems to perform the deletion of second (together with some of its cascaded deleted entities) BEFORE the deletion of First. I mean, the log shows a call to DELETE on the Second table, but no previous delete on the First table. This causes a foreign key constraint failure.

This seems to happen only sometimes. My question is:
  • how is it possible? I'm calling remove on First before calling remove on Second!
  • how to avoid this problem?

Thanks in advance,
Mauro


Back to the top