Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Problem with Master-Detail-Relationship

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




Back to the top