Re: Moving object between DataContext

From: Jonathan Bélisle (jonathan.belisl..cda.ca)
Date: Sun Jul 23 2006 - 13:57:11 EDT

  • Next message: Malcolm Edgar: "Re: Cayenne TSS thread"

    I need the second DataContext for the following reasons :

    I have a service class for business logic.
    It's the service class that is responsible for accessing the database.
    The UI layer calls the service class and gets a DataObject or a list of
    DataObjects.
    The UI Layer perform some modification on the objects and then call
    the service to save the data and perform extra work.

    For this to work, the dataobjects returned by the service must have a
    datacontext or I won't be able
    to modified the relationships in the UI layer.
    The second DataContext is needed when I call the service to save the
    data. I need to be sure that there is no other object in the dataContext
    that will be saved on commitChanges except the ones pass to the method.

    Class UserService {

        public UserDataObject getUserXXX(...) {
            List results=
    DataContext.getThreadDataContext().performQuery(new
    SelectQuery(User.class.....);
            ...
           return (UserDataObject) result.get(x);
        }

        public void saveUser(UserDataObject udo) {
            DataContext writeDc= DataContext.createDataContext();
            udo= writeDc.localObject(udo.getObjectId(), udo);
       
              ... Perform extra work, send email, log, extra validation, ...

             writeContext.commitChanges();
        }
    }

    Class UIWebPage {

        public doSomething() {
           UserDataObject udo= m_userService.getUserXXX(...);
           udo.setXXX(...);
           udo.setXXX(...);
           udo.setXXX(...);

           AppointementDataObject ado=
    m_appointementService.getAppointemetXXX(...);
           ado.setXXX(...);
           ado.setXXX(...);
           ado.setXXX(...);

           m_userService.saveUser(uod);
        }
    }

    If i don't use another dataContext in saveUser, the appointement will
    get saved too.
    What i really would like to be able to do, is to be able to
    disconnect/reconnect the dataObjects.
    This way, the service disconnect the dataObjects before returning them
    to the client and reconnect them when saving them.
    Is it possible with cayenne ?
    I have tried unregister/register the objects but you cannot modify the
    relationships when there is no dataContext.
    I also need cayenne to be able to update only the modified properties
    when i reconnect them, not all the properties.

    I've been struggling with this issue for a while. Can someone help me on
    this.
    I'm using cayenne in a project where multiple UI access the service
    layer; Tapestry and Swing.

    Jonathan.

    Michael Gentry wrote:
    > At the risk of asking silly questions, why do you need the second
    > DataContext? If you do need the second one, could you wait until you
    > pull the object into the second context to do the setters? (So the
    > second context sets modified correctly.)
    >
    > Your comment about Cayenne not thinking an object is modified is
    > correct. If you have:
    >
    > if (object.getFirstName().equals("Michael"))
    > {
    > object.setFirstName("michael");
    > object.setFirstName("Michael");
    > }
    >
    > You essentially haven't modified the object and Cayenne figures this out.
    >
    > Thanks,
    >
    > /dev/mrg
    >
    >
    > On 7/23/06, Jonathan Bélisle <jonathan.belisl..cda.ca> wrote:
    >> Hi, here is my problem.
    >>
    >> I have a DataObject da1 registered with DataContext dc1.
    >> I perform modification on da1. It's persistence state becomes MODIFIED.
    >>
    >> Now I want to move da1 to DataContext dc2 and keep it's state MODIFIED
    >> so that when I do dc2.commitChanges(); da1 get written to the database.
    >>
    >> Using dc2.localObject(da1.getObjectId(), da1) doesn't work because it's
    >> set the persistence state to COMMITTED and loose
    >> track of witch properties were modified.
    >> Even if I do da1.setPersistanceState(PersistenceState.MODIFIED) after
    >> localObject() da1 does not get written to the database on the next
    >> commit because dc2 thinks that no properties were modified.
    >>
    >> Anybody know how to do that, a workaround ?
    >>
    >> Thanks in advance, Jonathan.
    >>
    >>
    >>
    >>
    >>
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Sun Jul 23 2006 - 13:57:35 EDT