[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] Problem with Master-Detail-Relationship
|
Hello Michael,
This is expected because EclipseLink orders updates before it does
deletes - allowing users to clean up references to avoid constraint
issues that deletes might cause. There is a flag you can set to have
EclipseLink perform deletes first, set via the
UnitOfWork.setShouldPerformDeletesFirst(boolean). To get at the
UnitOfWork in JPA, you can use
UnitOfWork uow = (UnitOfWork)( (JpaEntityManager)em.getDelegate()
).getActiveSession( em );
uow.shouldPerformDeletesFirst();
I'm not sure of any other way to set the flag at the moment. As for
paterns/anti-paterns, the application should not set the field to null
if there is a not-null constraint on the field. While the remove may
happen immediately immediately after the null now, later on the
application may do other work in between that could cause or require a
flush, causing the same problem you are now in.
Best Regards,
Chris
Michael Simons wrote:
Hello,
We've got a typical master-detail-relationship like:
class A {
Set<B> details;
...
removeDetail (B b) {
boolean removed = details.remove (b);
if (removed)
b.setMaster (null);
return removed;
}
}
class B {
A master;
...
setMaster (A other) {
// if new master differs from old one we also remove
// this detail from the old master
if (master != null) {
if (!master.equals (other)) {
master.removeDetail (this);
master = other;
if (master != null)
master.addDetail (this);
}
} else if (other != null) {
master = other;
master.addDetail (this);
}
}
}
The problem occurs when a detail is removed persistently, because we first call
A.removeDetail(detailToRemove) before entityManager.remove (detailToRemove);
Doing so EclipseLink creates an update statement like
update detail_table set master_id = null where detail_id = ...;
before the delete statement for the detail record.
The update statement causes an exception, when the master_id column of the detail_table is NOT NULL.
Is this behaviour intended?
Is my code an anti-pattern?
Any hints are appreciated.
kind regards,
Michael
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users