You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Øyvind Harboe <oy...@zylin.com> on 2011/02/10 09:12:20 UTC

How to create a new datacontext based on a datacontext

In my app I'm creating a new datacontext based on a
existing datacontext, but the method I used to do this
is now deprecated and I don't know what to replace
it with:

getDataContext().getParentDataDomain().createDataContext();

?

-- 
Øyvind Harboe

Can Zylin Consulting help on your project?

US toll free 1-866-980-3434 / International +47 51 87 40 27

http://www.zylin.com/zy1000.html
ARM7 ARM9 ARM11 XScale Cortex
JTAG debugger and flash programmer

Re: How to create a new datacontext based on a datacontext

Posted by Øyvind Harboe <oy...@zylin.com>.
On Thu, Feb 10, 2011 at 2:27 PM, Michael Gentry <mg...@masslight.net> wrote:
> Which version of Cayenne are you using?  It isn't
> deprecated in Cayenne 3.0.

I'm trying to upgrade to Cayenne 3.1, so I want to stop
using it.




-- 
Øyvind Harboe

Can Zylin Consulting help on your project?

US toll free 1-866-980-3434 / International +47 51 87 40 27

http://www.zylin.com/zy1000.html
ARM7 ARM9 ARM11 XScale Cortex
JTAG debugger and flash programmer

Re: How to create a new datacontext based on a datacontext

Posted by Michael Gentry <mg...@masslight.net>.
Which version of Cayenne are you using?  It isn't deprecated in Cayenne 3.0.

mrg


On Thu, Feb 10, 2011 at 3:12 AM, Øyvind Harboe <oy...@zylin.com> wrote:
> In my app I'm creating a new datacontext based on a
> existing datacontext, but the method I used to do this
> is now deprecated and I don't know what to replace
> it with:
>
> getDataContext().getParentDataDomain().createDataContext();
>
> ?
>
> --
> Øyvind Harboe
>
> Can Zylin Consulting help on your project?
>
> US toll free 1-866-980-3434 / International +47 51 87 40 27
>
> http://www.zylin.com/zy1000.html
> ARM7 ARM9 ARM11 XScale Cortex
> JTAG debugger and flash programmer
>

Re: How to create a new datacontext based on a datacontext

Posted by Øyvind Harboe <oy...@zylin.com>.
Uhh.... injection again. Not in my repertoire as of writing :-)

I'm using this code from within a subclass of
CayenneDataObject, so I don't know who created the
DataContext that this CayenneDataObject lives in.

Can I get from a CayenneDataObject to a CayenneRuntime?


I coded up the below which allows me to create a new
DataContext with only the information in a DataContext:

	/** Duplicate code from deprecated method as we have no other way
	 * to create a new DataContext based on an existing one.
	 */
	public static DataContext createDataContext(DataContext dataContext)
	{
		DataDomain r = dataContext.getParentDataDomain();

		DataRowStore snapshotCache = r.isSharedCacheEnabled() ?
				r.getSharedSnapshotCache() :
			new DataRowStore(r.getName(), r.getProperties(), r.getEventManager());
		
		DataContext context = new DataContext(r, new ObjectStore(snapshotCache));
		
		context.setValidatingObjectsOnCommit(r.isValidatingObjectsOnCommit());
		return context;
	}


-- 
Øyvind Harboe

Can Zylin Consulting help on your project?

US toll free 1-866-980-3434 / International +47 51 87 40 27

http://www.zylin.com/zy1000.html
ARM7 ARM9 ARM11 XScale Cortex
JTAG debugger and flash programmer

Re: How to create a new datacontext based on a datacontext

Posted by Andrus Adamchik <an...@objectstyle.org>.
Yeah, one way is to reorg your code to make runtime accessible. An alternative would be to register a custom ObjectContextFactory with DI that will provide DataContext with access to runtime (or rather to org.apache.cayenne.di.Injector that is the configuration engine inside Runtime):

class MyDataContextFactory extends DataContextFactory {

 @Override
   protected ObjectContext createdFromDataDomain(DataDomain parent) {

        ObjectContext context = super. createdFromDataDomain(parent);
        context.setUserProperty("injector", injector);
        return context;
    }
}

// create a peer ObjectContext:

Injector i = (Injector) context.getUserProperty("injector");
ObjectContext peerContext = i.getInstance(ObjectContextFactory.class).createContext();


So that's an example of why DI is so cool (even in its current raw form) - you can have direct access to any piece of configuration and can change its behavior.

Andrus


On Feb 10, 2011, at 10:03 AM, Øyvind Harboe wrote:

> On Thu, Feb 10, 2011 at 3:57 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>> Content is created by a "runtime" object (normally a ServerRuntime) :
>> 
>>   ObjectContext context = cayenneRuntime.getContext();
>> 
>> See an example in tutorial's "Main" class.
> 
> Can I get to the cayenneRuntime based on a datacontext
> class?
> 
> Otherwise, I need to reorganize my program so as to push
> the cayenneRuntime  to the far reaches of the code that needs
> it.
> 
> -- 
> Øyvind Harboe
> 
> Can Zylin Consulting help on your project?
> 
> US toll free 1-866-980-3434 / International +47 51 87 40 27
> 
> http://www.zylin.com/zy1000.html
> ARM7 ARM9 ARM11 XScale Cortex
> JTAG debugger and flash programmer
> 


Re: How to create a new datacontext based on a datacontext

Posted by Øyvind Harboe <oy...@zylin.com>.
On Thu, Feb 10, 2011 at 3:57 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> Content is created by a "runtime" object (normally a ServerRuntime) :
>
>   ObjectContext context = cayenneRuntime.getContext();
>
> See an example in tutorial's "Main" class.

Can I get to the cayenneRuntime based on a datacontext
class?

Otherwise, I need to reorganize my program so as to push
the cayenneRuntime  to the far reaches of the code that needs
it.

-- 
Øyvind Harboe

Can Zylin Consulting help on your project?

US toll free 1-866-980-3434 / International +47 51 87 40 27

http://www.zylin.com/zy1000.html
ARM7 ARM9 ARM11 XScale Cortex
JTAG debugger and flash programmer

Re: How to create a new datacontext based on a datacontext

Posted by Andrus Adamchik <an...@objectstyle.org>.
Content is created by a "runtime" object (normally a ServerRuntime) :

   ObjectContext context = cayenneRuntime.getContext();

See an example in tutorial's "Main" class.

Andrus


On Feb 10, 2011, at 3:12 AM, Øyvind Harboe wrote:
> In my app I'm creating a new datacontext based on a
> existing datacontext, but the method I used to do this
> is now deprecated and I don't know what to replace
> it with:
> 
> getDataContext().getParentDataDomain().createDataContext();
> 
> ?
> 
> -- 
> Øyvind Harboe
> 
> Can Zylin Consulting help on your project?
> 
> US toll free 1-866-980-3434 / International +47 51 87 40 27
> 
> http://www.zylin.com/zy1000.html
> ARM7 ARM9 ARM11 XScale Cortex
> JTAG debugger and flash programmer
>