Re: different ObjectContext...

From: Marcin Skladaniec (poldwusmoko..mail.com)
Date: Mon Apr 26 2010 - 21:57:36 EDT

  • Next message: Andrus Adamchik: "Re: Fetching Specified "Raw Rows" with SelectQuery"

    Hi
    I'm not sure about the environment you are using, but there is an easier way. Lookup localObject(ObjectId id, Object prototype) in CayenneContext class.
    most of the time all you need to do is:

    PersistentObject copiedPO = context.localObject(originalPO.getObjectId(), null);

    Marcin

    On 24/04/2010, at 9:54 PM, Nishant Neeraj wrote:

    > Hi Arnaud,
    >
    > Sorry, did not see this post earlier.
    > We stumbled on this problem, especially when playing with browser back
    > button.
    > I am not sure if de-serialization causes re-bind of the Context to different
    > thread... specially when we have session level DataContext that stays alive
    > across the pages.
    >
    > Anyways, I did a work around for this. I wrote a LoadableDetachable type of
    > IModel that performs
    >
    > [CODE]
    > public T getObject() {
    > if (this.object == null) {
    > if (this.objectId != null) {
    > this.object = load(objectId);
    > if (this.object == null) {
    > throw new MyRuntimeException("Entity not found. Id: "
    > + this.objectId);
    > }
    > }
    > }
    >
    > if (this.object != null
    > && ((DataObject) object).getPersistenceState() ==
    > PersistenceState.HOLLOW
    > && this.objectId != null) {
    > this.object = load(this.objectId);
    > }
    >
    > return this.object;
    > }
    >
    > [/CODE]
    >
    >
    > And load is something like this
    >
    > [CODE]
    > private T load(ObjectId objId) {
    > DataContext ctx = ((CayenneORMInterface) EduShareSession.get()
    > .getSessionORMInterface()).getDataContext();
    > DataObject dobj = (DataObject) DataObjectUtils.objectForPK(ctx,
    > objId);
    > return (T) dobj;
    > }
    > [/CODE]
    >
    > This works most of the cases.
    >
    > However, I am still looking for a better solution because if you perform
    > DataContext.newObject(...) -- the object, when serialized without
    > DataContext.commitChanges() -- does not hold a permanentId (temporary object
    > ID is not much help). And you can't ask Cayenne to resurrect this from Cache
    > based on temporaryId.
    >
    >
    > Hopefully this helps. Suggestions welcome.
    >
    > - Nishant
    >
    > On Fri, Apr 23, 2010 at 12:43 AM, Arnaud Garcia <arnau..magemed-87.com>wrote:
    >
    >> Hello,
    >> (lot of questions today ;-)
    >>
    >> I have a curious error on my site (wicket+cayenne).
    >>
    >> Cannot set object as destination of relationship toAdresse because it is in
    >> a different ObjectContext
    >> at
    >>
    >> org.apache.cayenne.CayenneDataObject.willConnect(CayenneDataObject.java:344)
    >> at
    >>
    >> org.apache.cayenne.CayenneDataObject.setToOneTarget(CayenneDataObject.java:298)
    >>
    >>
    >>
    >> How a cayenne object can be in a different context ? (In my understanding a
    >> Context is attached to the current thread, so different ObjectContext means
    >> different threads)
    >>
    >> Is it possible that the Wicket serialize/de-serialize mechanism create a
    >> new re-bind of the Context to another thread ??
    >> there was a similar discussion 2 weeks ago:
    >>
    >> http://cayenne.195.n3.nabble.com/Conditions-when-PersistenceState-gets-hollow-tp691334p692371.html
    >>
    >>
    >> thanks
    >>
    >> Arnaud
    >>



    This archive was generated by hypermail 2.0.0 : Mon Apr 26 2010 - 21:58:27 EDT