You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Michael Lepine <mi...@gmail.com> on 2007/04/07 19:59:05 UTC

Delete query not generating as expected

I'm experiencing something confusing when committing changes with a
DataContext and objects to be deleted. I have a list of CayenneDataObjects
that I'm either updating or deleting depending on parameters set in a web
client. I've included the code below.

When I debug the code, I can see the PersistanceState of the objects.
Initially, the PersistanceState is COMMITTED. After the for loop, the
objects that did have a delete flag set, do have a PersistanceState of
DELETED (as expected). So, when the DataContext.commitChanges() method
executes, it only generates and runs the update query. It does not generate
the delete query as I'd expect it to. After the commitChanges() executes,
the objects that had a PersistanceState of DELETED now have a
PersistanceState of TRANSIENT, and obviously, the records still exist in the
database.

Does anyone have any ideas that I could try? This is driving me crazy. I'm
sure I'm missing something very simple.

Thanks for any help you can offer.


// MerchElementSupportedAnswer is a subclass of CayenneDataObject
*for* (MerchElementSupportedAnswer answer : answers) {

    // if data for answer was deleted, delete record

*    if* (answer.isDeleteFlag()) {

        answer.setPersistenceState(PersistenceState.*DELETED*);

*        continue*;

    }

    answer.setToMerchElementDefinitions(getElementDefinition(dataContext));

    answer.setLastUpdatedDate(lastUpdatedDate);

}

dataContext.commitChanges();

Re: Delete query not generating as expected

Posted by Michael Lepine <mi...@gmail.com>.
Jackpot! That was it. Can't believe I mixed that up. Thank you so much.



On 4/7/07, Michael Gentry <bl...@gmail.com> wrote:
>
> It looks like you are setting the persistence state manually to
> DELETED.  You shouldn't really need to ever modify the persistence
> state yourself (Cayenne manages it), although sometimes it is useful
> to get the state.  Instead, you need to tell the DataContext to delete
> the object -- just like you tell the DataContext to create new objects
> and to perform queries.
>
> Try changing:
>
> answer.setPersistenceState(PersistenceState.DELETED);
>
> to:
>
> dataContext.deleteObject(answer);
>
> /dev/mrg
>
>
> On 4/7/07, Michael Lepine <mi...@gmail.com> wrote:
> > I'm experiencing something confusing when committing changes with a
> > DataContext and objects to be deleted. I have a list of
> CayenneDataObjects
> > that I'm either updating or deleting depending on parameters set in a
> web
> > client. I've included the code below.
> >
> > When I debug the code, I can see the PersistanceState of the objects.
> > Initially, the PersistanceState is COMMITTED. After the for loop, the
> > objects that did have a delete flag set, do have a PersistanceState of
> > DELETED (as expected). So, when the DataContext.commitChanges() method
> > executes, it only generates and runs the update query. It does not
> generate
> > the delete query as I'd expect it to. After the commitChanges()
> executes,
> > the objects that had a PersistanceState of DELETED now have a
> > PersistanceState of TRANSIENT, and obviously, the records still exist in
> the
> > database.
> >
> > Does anyone have any ideas that I could try? This is driving me crazy.
> I'm
> > sure I'm missing something very simple.
> >
> > Thanks for any help you can offer.
> >
> >
> > // MerchElementSupportedAnswer is a subclass of CayenneDataObject
> > *for* (MerchElementSupportedAnswer answer : answers) {
> >
> >     // if data for answer was deleted, delete record
> >
> > *    if* (answer.isDeleteFlag()) {
> >
> >         answer.setPersistenceState(PersistenceState.*DELETED*);
> >
> > *        continue*;
> >
> >     }
> >
> >     answer.setToMerchElementDefinitions
> (getElementDefinition(dataContext));
> >
> >     answer.setLastUpdatedDate(lastUpdatedDate);
> >
> > }
> >
> > dataContext.commitChanges();
> >
>

Re: Delete query not generating as expected

Posted by Michael Gentry <bl...@gmail.com>.
It looks like you are setting the persistence state manually to
DELETED.  You shouldn't really need to ever modify the persistence
state yourself (Cayenne manages it), although sometimes it is useful
to get the state.  Instead, you need to tell the DataContext to delete
the object -- just like you tell the DataContext to create new objects
and to perform queries.

Try changing:

answer.setPersistenceState(PersistenceState.DELETED);

to:

dataContext.deleteObject(answer);

/dev/mrg


On 4/7/07, Michael Lepine <mi...@gmail.com> wrote:
> I'm experiencing something confusing when committing changes with a
> DataContext and objects to be deleted. I have a list of CayenneDataObjects
> that I'm either updating or deleting depending on parameters set in a web
> client. I've included the code below.
>
> When I debug the code, I can see the PersistanceState of the objects.
> Initially, the PersistanceState is COMMITTED. After the for loop, the
> objects that did have a delete flag set, do have a PersistanceState of
> DELETED (as expected). So, when the DataContext.commitChanges() method
> executes, it only generates and runs the update query. It does not generate
> the delete query as I'd expect it to. After the commitChanges() executes,
> the objects that had a PersistanceState of DELETED now have a
> PersistanceState of TRANSIENT, and obviously, the records still exist in the
> database.
>
> Does anyone have any ideas that I could try? This is driving me crazy. I'm
> sure I'm missing something very simple.
>
> Thanks for any help you can offer.
>
>
> // MerchElementSupportedAnswer is a subclass of CayenneDataObject
> *for* (MerchElementSupportedAnswer answer : answers) {
>
>     // if data for answer was deleted, delete record
>
> *    if* (answer.isDeleteFlag()) {
>
>         answer.setPersistenceState(PersistenceState.*DELETED*);
>
> *        continue*;
>
>     }
>
>     answer.setToMerchElementDefinitions(getElementDefinition(dataContext));
>
>     answer.setLastUpdatedDate(lastUpdatedDate);
>
> }
>
> dataContext.commitChanges();
>