The problem is in updateList. In the current released version (1.0a4),
when a toMany relationship (in this case toEmails) is modified, the base
object (mlist) is not marked as MODIFIED because none of it's attributes
have changed.
This has been changed in the current version in CVS (due for release in
about a week I believe) to support 'rollbackChanges' on DataContext.
In your particular case, you probably don't need to check the
persistence state at all in updateList, except for the first TRANSIENT
state. Just call commitChanges anyway. This will insert any new
objects that need it (in NEW state), and any MODIFIED objects will be
updated, IF any of their attributes have changed. In the case below,
assuming nothing else changed in mlist other than adding the CmEmail, no
update would be performed for mlist (cayenne is smart like that :-)).
updateList would end up looking something like:
void updateList(CmList mlist) {
// ctxt defined above
//check if it is a Transient object!!!
if(mlist.getPersistenceState() == PersistenceState.TRANSIENT){
System.out.println(">>>>>>>>>>>>>>>>>>>> register transient maillist:"+mlist);
//register transient object
ctxt.registerNewObject(mlist, "CmMaillist");
System.out.println(">>>>>>>>>>>>>>>>>>>> commiting transient maillist:"+mlist);
}
ctxt.commitChanges();
}
Hope this helps,
Craig Miskell
On Thu, 2002-12-12 at 01:00, ti..e7en.ru wrote:
> Hello cayenne-user,
>
> I have two linked tables
> CmList(id,title) and CmEmail(id,email,list_id)
>
> When I add to an existing CmList new CmEmail, and then I make commit
> addition does not occur.
>
> ... CmList mlist defined above ...
> addEmail(mlist);
> updateList(mlist);
>
> and nothing happens :-(((
>
>
> void addEmail(CmList mlist) {
> // new email object
> CmEmail email = (CmEmail)mlist.getDataContext().createAndRegisterNewObject("CmEmail");
> // or CmEmail email = (CmEmail)ctxt.createAndRegisterNewObject("CmEmail");
> // make link to list
> mlist.addToEmails(email);
> }
>
> void updateList(CmList mlist) {
> // ctxt defined above
> //check if it is a Transient object!!!
> if(mlist.getPersistenceState() == PersistenceState.TRANSIENT){
> System.out.println(">>>>>>>>>>>>>>>>>>>> register transient maillist:"+mlist);
> //register transient object
> ctxt.registerNewObject(mlist, "CmMaillist");
> System.out.println(">>>>>>>>>>>>>>>>>>>> commiting transient maillist:"+mlist);
> ctxt.commitChanges();
> }
> //check whether modified
> if(mlist.getPersistenceState() == PersistenceState.MODIFIED){
> System.out.println(">>>>>>>>>>>>>>>>>>>> commiting modified maillist:"+mlist);
> //commit it
> mlist.getDataContext().commitChanges();
> }
> //check whether new
> if(mlist.getPersistenceState() == PersistenceState.NEW){
> System.out.println(">>>>>>>>>>>>>>>>>>>> commiting new maillist:"+mlist);
> //commit it
> mlist.getDataContext().commitChanges();
> }
> }
>
> --
> Best regards, tim mailto:ti..e7en.ru
>
This archive was generated by hypermail 2.0.0 : Wed Dec 11 2002 - 14:08:33 EST