NullPointerException, because of relationship cycles before deleting objects
----------------------------------------------------------------------------

         Key: CAY-714
         URL: http://issues.apache.org/cayenne/browse/CAY-714
     Project: Cayenne
        Type: Bug

  Components: Cayenne Core Library  
    Versions: 1.2 [STABLE]    
 Environment: cayenne-1.2.1, derby
    Reporter: Peter Karich


The following code:
-------------------------------------------------
ArrayList temporaryObjects = new ArrayList();
DataContext context = DataContext.createDataContext();

Room r = (Room) context.createAndRegisterNewObject(Room.class);
r.setName("testRoom");
temporaryObjects.add(r);

TimeInterval ti = 
(TimeInterval)context.createAndRegisterNewObject(TimeInterval.class);
temporaryObjects.add(ti);
ti.setToOneTarget("room", r, false);

context.deleteObjects(temporaryObjects);
temporaryObjects.clear();

context.commitChanges();
-------------------------------------------------

will throw a NPE at 'context.deleteObjects':

-------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
        at 
org.objectstyle.cayenne.CayenneDataObject.removeToManyTarget(CayenneDataObject.java:281)
        at 
org.objectstyle.cayenne.access.DataContextDeleteAction.processDeleteRules(DataContextDeleteAction.java:239)
        at 
org.objectstyle.cayenne.access.DataContextDeleteAction.deleteNew(DataContextDeleteAction.java:149)
        at 
org.objectstyle.cayenne.access.DataContextDeleteAction.performDelete(DataContextDeleteAction.java:131)
        at 
org.objectstyle.cayenne.access.DataContext.deleteObject(DataContext.java:1043)
        at 
org.objectstyle.cayenne.access.DataContext.deleteObjects(DataContext.java:1027)
-------------------------------------------------

Here the mapping:

-------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<data-map project-version="1.1">
    <property name="defaultPackage" value="caybug"/>
    <db-entity name="ROOM">
        <db-attribute name="NAME" type="VARCHAR" length="24"/>
        <db-attribute name="R_ID" type="INTEGER" isPrimaryKey="true" 
isMandatory="true"/>
    </db-entity>
    <db-entity name="TIME_INTERVAL">
        <db-attribute name="ROOM_FK" type="INTEGER"/>
        <db-attribute name="TI_ID" type="INTEGER" isPrimaryKey="true" 
isMandatory="true"/>
    </db-entity>
    <obj-entity name="Room" className="caybug.Room" dbEntityName="ROOM">
        <obj-attribute name="name" type="java.lang.String" 
db-attribute-path="NAME"/>
    </obj-entity>
    <obj-entity name="TimeInterval" className="caybug.TimeInterval" 
dbEntityName="TIME_INTERVAL">
    </obj-entity>
    <db-relationship name="TIME_INTERVALS" source="ROOM" target="TIME_INTERVAL" 
toMany="true">
        <db-attribute-pair source="R_ID" target="ROOM_FK"/>
    </db-relationship>
    <db-relationship name="ROOM" source="TIME_INTERVAL" target="ROOM" 
toMany="false">
        <db-attribute-pair source="ROOM_FK" target="R_ID"/>
    </db-relationship>
    <obj-relationship name="timeIntervals" source="Room" target="TimeInterval" 
deleteRule="Nullify" db-relationship-path="TIME_INTERVALS"/>
    <obj-relationship name="room" source="TimeInterval" target="Room" 
deleteRule="Nullify" db-relationship-path="ROOM"/>
</data-map> 
-------------------------------------------------

A temporary fix is to break the cycle before every delete:

ti.setToOneTarget("room", null, false); 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/cayenne/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to