Re: NullPointerException when setting deletion rules

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Dec 01 2006 - 06:53:31 EST

  • Next message: Michael Gentry: "Re: Re: best practice for handling database schema changes"

    Hmm... I'd say Cayenne should be smarter about this case. Could you
    please open a bug report?

    In the meantime you may have to manually break relationship cycles
    before deleting objects. E.g.:

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

    Andrus

    On Nov 29, 2006, at 1:48 AM, Peter wrote:
    > Hallo!
    >
    > I reduced my application code to the following:
    >
    > -------------------------------------------------
    > 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();
    > -------------------------------------------------
    >
    > where I get 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.processDeleteRu
    > les(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)
    > -------------------------------------------------
    >
    > I don't get this if I do
    > ti.setToOneTarget("room", r, -->true<--);
    >
    > But I need both: uni AND bidirectional relationships!
    > 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>
    > -------------------------------------------------
    >
    > What is wrong? I used the default generated Room and TimeInterval
    > classes.
    >
    > Thank you, Peter.



    This archive was generated by hypermail 2.0.0 : Fri Dec 01 2006 - 06:54:02 EST