Composition and Association Relations

Child objects in a composition are deleted by NDO if the parent object is deleted. Hence a child object must not be included in more than a single composition relation. To make sure that this requirement is met, an object that is added to a list in the parent object must be transient. Let’s assume the class Employee contains this code:

class Employee

{

    [NDORelation(typeof(Travel), RelationInfo.Composite)]

    IList travels = new ArrayList();

    public void AddTravel(Travel t)

    {

        travels.Add(t);

    }

}

// In your application

...

void foo()

{

   Employee emp = ...;

   Travel t = new Travel

   pm.MakePersistent(t);

   // causes a NDOException

   emp.AddTravel(t);

}

 

 

The application code causes an exception since t as a persistent object could be associated to another parent object.

Just the opposite situation occurs with an association (see the altered implementation of Employee below). The t object is transient, since the foo() method does n’t call MakePersistent. That would cause an exception, since t must be an independent object.

class Employee

{

    [NDORelation(typeof(Travel))]

    IList travels = new ArrayList();

    public void AddTravel(Travel t)

    {

        travels.Add(t);

    }

}

// In your application

...

void foo()

{

   Employee emp = ...;

   Travel t = new Travel

   // causes a NDOException

   emp.AddTravel(t);

}

Deleting objects in Composite Relations

Objects in composite relations cannot be deleted with the PersistenceManager function Delete(). Instead just remove these objects from the child objects list, or, in the case of a relation with cardinality 1 just set the relation field to null. Then NDO deletes the objects the next time Save()is called and makes them transient.

Deleting objects in Associations

Child objects in associations are independent objects. They won't be deleted if you remove them from the child object list or null the relation field. Before you remove such objects with the PersistenceManager method Delete(), make sure that the objects are removed from all relations where they could be included. Otherwise your stored data become inconsistent (foreign keys pointing to non existing rows). There are cases, in which NDO is able to detect this situation and to throw an exeption. But this is not possible in all cases.