FAQ
i am not quite sure, but i think that you have to use

context.createAndRegister(MyType.class);

instead of

context.newObject(MyType.class);


-----Ursprüngliche Nachricht-----
Von: Eric Floehr
Gesendet: Dienstag, 6. März 2007 19:37
An: user@cayenne.apache.org
Betreff: Help: getting NPE at ObjectStoreGraphDiff.preprocess

Hi!



I am new to Cayenne, and am having some problems. I have been able to
populate some DB records, but when I try to set up a relationship, I get
an NPE on the commit. I am using Cayenne 2.0.2.



Here is the essential code (with some names changed to protect the
innocent):



MyType type = (MyType) context.newObject(MyType.class);

type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
MyType.MYTYPE_ID_PK_COLUMN, 1));

type.setName("MyName");

MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
MyCategory.class, 1);

type.setMyCategory(category);

context.commitChanges();



The MyType table has fields mytype_id (the primary key), name, and a
mycategory_id (foreign key to the mycategory table). I am creating a
new "MyType" object with a mytype_id of 1, name of "MyName", and
pointing to a Category object, that is already in the database, with a
primary key of 1.



When commitChanges() runs, I get:



Exception in thread "main" java.lang.NullPointerException

at
org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
phDiff.java:184)

at
org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
ff.java:53)

at
org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)

at
org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
8)

at
org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
0)

at
com.boxicom.server.db.CreateDb.testStreamTypeAdd(CreateDb.java:96)

at com.boxicom.server.db.CreateDb.main(CreateDb.java:105)



Can anyone help me determine what is going wrong?



Thanks much!

Eric

Search Discussions

  • Eric Floehr at Mar 7, 2007 at 1:22 pm
    Thanks for the help, Peter! However, when I change:

    MyType type = (MyType) context.newObject(MyType.class);

    to:

    MyType type = (MyType) context. createAndRegisterNewObject(MyType.class);

    I still end up with the same NPE:

    Exception in thread "main" java.lang.NullPointerException
    at org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGraphDiff.java:184)
    at org.apache.cayenne.access.ObjectStoreGraphDiff.(ObjectStore.java:496)
    at org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:1208)
    at org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:1130)
    ...

    -Eric


    -----Original Message-----
    From: Peter Schröder
    Sent: Wednesday, March 07, 2007 2:20 AM
    To: user@cayenne.apache.org
    Subject: AW: Help: getting NPE at ObjectStoreGraphDiff.preprocess

    i am not quite sure, but i think that you have to use

    context.createAndRegister(MyType.class);

    instead of

    context.newObject(MyType.class);


    -----Ursprüngliche Nachricht-----
    Von: Eric Floehr
    Gesendet: Dienstag, 6. März 2007 19:37
    An: user@cayenne.apache.org
    Betreff: Help: getting NPE at ObjectStoreGraphDiff.preprocess

    Hi!



    I am new to Cayenne, and am having some problems. I have been able to
    populate some DB records, but when I try to set up a relationship, I get
    an NPE on the commit. I am using Cayenne 2.0.2.



    Here is the essential code (with some names changed to protect the
    innocent):



    MyType type = (MyType) context.newObject(MyType.class);

    type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
    MyType.MYTYPE_ID_PK_COLUMN, 1));

    type.setName("MyName");

    MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
    MyCategory.class, 1);

    type.setMyCategory(category);

    context.commitChanges();



    The MyType table has fields mytype_id (the primary key), name, and a
    mycategory_id (foreign key to the mycategory table). I am creating a
    new "MyType" object with a mytype_id of 1, name of "MyName", and
    pointing to a Category object, that is already in the database, with a
    primary key of 1.



    When commitChanges() runs, I get:



    Exception in thread "main" java.lang.NullPointerException

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
    phDiff.java:184)

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
    ff.java:53)

    at
    org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)

    at
    org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
    8)

    at
    org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
    0)

    at
    com.boxicom.server.db.CreateDb.testStreamTypeAdd(CreateDb.java:96)

    at com.boxicom.server.db.CreateDb.main(CreateDb.java:105)



    Can anyone help me determine what is going wrong?



    Thanks much!

    Eric
  • Andrus Adamchik at Mar 7, 2007 at 1:39 pm

    On Mar 7, 2007, at 9:19 AM, Peter Schröder wrote:

    i am not quite sure, but i think that you have to use

    context.createAndRegister(MyType.class);

    instead of

    context.newObject(MyType.class);
    No, the two are equivalent. In fact the first (older) form is
    deprecated in 3.0

    Andrus

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedMar 7, '07 at 7:19a
activeMar 7, '07 at 1:39p
posts3
users3
websitecayenne.apache.org

People

Translate

site design / logo © 2022 Grokbase