Re: Problems with CommitChanges and object PersistenceState

From: laila paganelli (laila.paganell..mail.com)
Date: Mon Jan 31 2005 - 05:53:36 EST

  • Next message: Andrus Adamchik: "Re: Problems with CommitChanges and object PersistenceState"

    Hi Andrus,

    how you suggest, I read ObjectStore.objectsCommitted and now I'm
    thinking the problem is in how i create and set the primary key.
    How to set a new object's id?

    I report the entire code and the output logger (I have upgraded the
    cayenne version to Cayenne 1.1)
            
                    org.objectstyle.cayenne.access.DataContext context;
                    com.extrapola.scooter.dataModel.PendingRawNews pendingRawNews;
                    org.objectstyle.cayenne.ObjectId key;
                    org.objectstyle.cayenne.query.SQLTemplate query;
                    org.objectstyle.cayenne.DataRow dataRow;
                    java.lang.Integer id;
                    
                    context = org.objectstyle.cayenne.access.DataContext.createDataContext();
                    
                    // generate new PK
                    query = new org.objectstyle.cayenne.query.SQLTemplate( com.extrapola.scooter.dataModel.RawNews.class,
                                                                                                                                    "SELECT #result('rawnws_seq.nextval'
    'java.lang.Integer' 'ID') FROM DUAL",
                                                                                                                                    true);
                    query.setFetchingDataRows(true);
                    dataRow = (org.objectstyle.cayenne.DataRow)context.performQuery(query).get(0);
                    id = (java.lang.Integer)dataRow.get("ID");
                    
                    // create a new ObjectId
                    key = new org.objectstyle.cayenne.ObjectId(
    com.extrapola.scooter.dataModel.PendingRawNews.class,
                                                                                                            com.extrapola.scooter.dataModel.PendingRawNews.ID_PK_COLUMN,
                                                                                                            id.intValue());
                                                                                                            
                    // create and register new object and setting new key
                    pendingRawNews =
    (com.extrapola.scooter.dataModel.PendingRawNews)context.createAndRegisterNewObject(com.extrapola.scooter.dataModel.PendingRawNews.class);
                    pendingRawNews.setObjectId(key);
                    pendingRawNews.setInsertionDate(new java.util.Date());
                    context.commitChanges();
                    
                    System.out.println("-----------\n" + context.newObjects());

    INFO 2005-01-31 11:32:27,957 --- will run 1 query.
    INFO 2005-01-31 11:32:29,653 --- transaction started.
    INFO 2005-01-31 11:32:30,200 SELECT rawnws_seq.nextval AS ID FROM DUAL
    INFO 2005-01-31 11:32:30,282 === returned 1 row. - took 81 ms.
    INFO 2005-01-31 11:32:30,285 +++ transaction committed.
    DEBUG 2005-01-31 11:32:30,441 Creating InsertBatchQuery for DbEntity PNDRAWNWS
    INFO 2005-01-31 11:32:30,454 --- transaction started.
    INFO 2005-01-31 11:32:30,455 --- will run 1 query.
    INFO 2005-01-31 11:32:30,461 INSERT INTO PNDRAWNWS (ID,
    INSERTION_DATE) VALUES (?, ?)
    INFO 2005-01-31 11:32:30,463 [bind: 2066195, '2005-01-31 11:32:30.294']
    INFO 2005-01-31 11:32:30,473 === updated 1 row.
    INFO 2005-01-31 11:32:30,476 +++ transaction committed.
    -----------
    [{[ insertionDate => Mon Jan 31 11:32:30 CET 2005
    ]<oid: com.extrapola.scooter.dataModel.PendingRawNews: <ID: 2066195>;
    state: new>}
    ]

    Thanks
    Laila

    On Fri, 28 Jan 2005 09:02:54 -0500 (EST), Andrus Adamchik
    <andru..bjectstyle.org> wrote:
    > Hi Laila,
    >
    > That's bizarre... Does it also happen with Cayenne 1.1 final? Also can you
    > turn on SQL logging and post the full output during commit, including PK
    > generation phase?
    >
    > If this doesn't help, please open a bug report with relevant mapping and
    > sample code attached... Looking at ObjectStore.objectsCommitted(..), I can
    > see one place that *theoretically* can lead to this problem, but I can't
    > think of a reproducable test case for it...
    >
    > Thanks
    > Andrus
    >
    >
    > > The context used is the same everywhere.
    > > This is the correct code example, where I add a System.out.println after
    > > the first commit
    > >
    > > context =
    > > org.objectstyle.cayenne.access.DataContext.createDataContext(); prn =
    > > (PendingRawNews)context.createAndRegisterNewObject(PendingRawNews.class);
    > > context.commitChanges();
    > > System.out.println(context.newObjects());
    > >
    > > // ..... create other objects in the same context
    > >
    > > context.commitChanges();
    > >
    > >
    > > This is the output of the println
    > >
    > > [{
    > > [insertionDate => Fri Jan 28 09:43:29 CET 2005]
    > > <oid: com.extrapola.scooter.dataModel.PendingRawNews: <ID: 982>;
    > > state: new>
    > > }]
    > >
    > > I'm using cayenne 1.1rc2
    > >
    > > Thank's
    > > Laila
    > >
    > >
    > > On Thu, 27 Jan 2005 23:38:51 -0500, Andrus Adamchik
    > > <andru..bjectstyle.org> wrote:
    > >>
    > >> On Jan 27, 2005, at 7:48 AM, laila paganelli wrote:
    > >>
    > >> > context =
    > >> > org.objectstyle.cayenne.access.DataContext.createDataContext();
    > >> > prn =
    > >> > (PendingRawNews)this.dataContext().createAndRegisterNewObject(PendingRa
    > >> wNews.class);
    > >>
    > >>
    > >> > After the first commit the object prn is saved on the database, but
    > >> the state is still PersistenceState.NEW, while I expect it to be
    > >> PersistenceState.COMMITTED.
    > >> > After the second commit I find one more prn object on database.
    > >> Why? What is wrong?
    > >>
    > >> Is it possible that "context" in the first line and
    > >> "this.dataContext()" on the second are two different objects? Try
    > >> rewriting the code to make sure the same context is used everywhere.
    > >> E.g.:
    > >>
    > >> context = DataContext.createDataContext();
    > >> prn = (PendingRawNews)
    > >> context.createAndRegisterNewObject(PendingRawNews.class);
    > >>
    > >> Cheers,
    > >> Andrus
    > >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Mon Jan 31 2005 - 05:53:43 EST