Skip Headers

# Using deleteAllObjects With addConstraintDependencies

Previous

Next

In the second option, keep the one-to-many (A to B) relationship privately owned and add a constraint dependency from A to C, as shown in the following example:

    session.getDescriptor(A.class).addConstraintDependencies(C.class);

Now the delete code would be:

    uow.deleteObject(existingA);
    // delete one of the Cs
    uow.deleteObject(((B) existingA.getBs().get(1)).getC());

This option produces the following SQL:

    DELETE FROM B WHERE (A = 1)
    DELETE FROM A WHERE (ID = 1)
    DELETE FROM C WHERE (ID = 1)

In both cases, the B object is deleted before A and C. The main difference is that the second option generates fewer SQL statements, as it knows that it is deleting the entire set of Bs related from A.