You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Marcin Skladaniec <ma...@ish.com.au> on 2007/06/02 03:43:19 UTC

weird problems with context

Hi
I'm having a strange problem. When executing this bit of code:

List categories = aContext.performQuery(new SelectQuery 
(Category.class));
if (categories.size() != 0) {
	for (int categoriesCount = 0; categoriesCount < categories.size();  
categoriesCount++) {
	 	Category cat = (Category) categories.get(categoriesCount);
		logger.debug("context equal ? " + cat.getObjectContext().equals 
(aContext));
	}
}
							
the contexts are not equal, and I cant find the reason why !

I found this problem, because the perPersist() callback is not fired  
when I do
cat.getObjectContext().newObject(SomeEntity.class)
but it is when
aContext.newObject(SomeEntity.class)
to add complexity, postPersist() and other callbacks are fired in  
both cases.

Any clues ?

Marcin




Re: weird problems with context

Posted by Lachlan Deck <la...@gmail.com>.
Hi Marcin,

On 02/06/2007, at 11:43 AM, Marcin Skladaniec wrote:

> I'm having a strange problem. When executing this bit of code:
>
> List categories = aContext.performQuery(new SelectQuery 
> (Category.class));
> if (categories.size() != 0) {
> 	for (int categoriesCount = 0; categoriesCount < categories.size();  
> categoriesCount++) {
> 	 	Category cat = (Category) categories.get(categoriesCount);
> 		logger.debug("context equal ? " + cat.getObjectContext().equals 
> (aContext));
> 	}
> }

Try this:
logger.debug("context equal ? " + aContext == cat.getObjectContext());

The spanner in the works here (on the server-side) is the use of the  
ObjectContextCallbackInterceptor (see bottom of http:// 
cayenne.apache.org/doc/lifecycle-callbacks.html) which doesn't  
override equals.

Thus, you might also want to try:
if (aContext instanceof ObjectContextCallbackInterceptor)
	logger.debug("context equal ? " + ((ObjectContextCallbackInterceptor) 
aContext).getContext() == cat.getObjectContext());
else
	logger.debug("context equal ? " + aContext == cat.getObjectContext());

(ps, svn up... I've added it to our ObjectContextCallbackInterceptor  
subclass ;-) But it'll be worth submitting a bugreport for this.

> the contexts are not equal, and I cant find the reason why !
>
> I found this problem, because the perPersist() callback is not  
> fired when I do
> cat.getObjectContext().newObject(SomeEntity.class)
> but it is when
> aContext.newObject(SomeEntity.class)
> to add complexity, postPersist() and other callbacks are fired in  
> both cases.

with regards,
--

Lachlan Deck



Re: weird problems with context

Posted by Andrus Adamchik <an...@objectstyle.org>.
Not to say that decorators (or any other single pattern) is a  
panacea. Often it is hard to achieve what you want simply by  
intercepting public methods, but I've always ran into problems when  
designing and coding delegate extension points...

Andrus


On Jun 5, 2007, at 5:57 PM, Andrus Adamchik wrote:

> [taking to dev]
>
> Delegation model hardcodes a preset number of extension points and  
> adds a fair amount of internal code registering delegates and  
> integrating them into the flow. I saw it falling apart many times  
> when trying to extend/change things in the stack.
>
> An alternative is to define all main stack components as  
> interfaces, and allow both end-users and Cayenne developers to  
> write decorators (as in "decorator pattern") to build loosely- 
> coupled functionality layers. Decorators (or interceptors, or  
> wrappers) follow the natural structure of the stack interfaces, do  
> not require special hardcoded flow, and generally do not increase  
> internal code complexity (allowing for better modularity). Think of  
> that as a cheap version of AOP or Objective C categories.
>
> BTW, JPA callback API itself is an example of the delegation model,  
> which is fine - I think this is an example of its appropriate use.  
> While for instance DataContextDelegate is questionably less  
> flexible than an ObjectContext decorator.
>
> Andrus
>
>
> On Jun 5, 2007, at 4:47 PM, Lachlan Deck wrote:
>> On 05/06/2007, at 10:31 PM, Andrus Adamchik wrote:
>>
>>> On Jun 5, 2007, at 3:22 PM, Lachlan Deck wrote:
>>>
>>>> Perhaps it'd make sense to be able to register a delegate...  
>>>> then you can still separate concerns ;-)
>>>
>>> Could you elaborate some more?
>>>
>>> Most often than not I find the EOF-like delegate approach to be  
>>> rather inflexible
>>
>> In what sense? I find it's a good fit when you need to insert some  
>> shared logic within the call logic. However...
>>
>>> , and certainly not suitable for the framework internal needs.
>>
>> The other option presented in EOF which is also useful is that of  
>> notifications. The difference between the two is usually that the  
>> delegate can abort an operation or provide custom shared behaviour  
>> whereas numerous objects can register as notification observers to  
>> run their own logic based on the transpired events. So perhaps  
>> notifications could be a good choice as any object can register  
>> themselves as an observer of a notification, registering the  
>> method to call when the notification is fired, and whether to  
>> limit the notification to those fired from a particular object or  
>> for any for that key. This seems to me to suit the lifecycle  
>> events nicely.
>>
>> Not only do individual PersistentObjects need to be notified of  
>> prePersist/postPersist etc but I find it useful (in the EOF space)  
>> of receiving a notification, when an editing context has been  
>> saved, that provides with the notification the list of objects  
>> deleted, inserted, and updated so as to deal with them as a group  
>> (which could handle fulfilling the former by interating through  
>> the objects can calling pre*/post*). But being able to register  
>> any object against these various lifecycle events ought to provide  
>> for additional flexibility.
>>
>> Btw, I'm thinking that PrePersist ought to be run just prior to  
>> persisting an object (as the name suggests) and that another  
>> postCreate lifecycle event ought to take its place. What do you  
>> think? That seems to be the missing piece in the puzzle between  
>> validateFor* and committing a new object.
>>
>> with regards,
>> --
>>
>> Lachlan Deck
>
>


Re: weird problems with context

Posted by Andrus Adamchik <an...@objectstyle.org>.
[taking to dev]

Delegation model hardcodes a preset number of extension points and  
adds a fair amount of internal code registering delegates and  
integrating them into the flow. I saw it falling apart many times  
when trying to extend/change things in the stack.

An alternative is to define all main stack components as interfaces,  
and allow both end-users and Cayenne developers to write decorators  
(as in "decorator pattern") to build loosely-coupled functionality  
layers. Decorators (or interceptors, or wrappers) follow the natural  
structure of the stack interfaces, do not require special hardcoded  
flow, and generally do not increase internal code complexity  
(allowing for better modularity). Think of that as a cheap version of  
AOP or Objective C categories.

BTW, JPA callback API itself is an example of the delegation model,  
which is fine - I think this is an example of its appropriate use.  
While for instance DataContextDelegate is questionably less flexible  
than an ObjectContext decorator.

Andrus


On Jun 5, 2007, at 4:47 PM, Lachlan Deck wrote:
> On 05/06/2007, at 10:31 PM, Andrus Adamchik wrote:
>
>> On Jun 5, 2007, at 3:22 PM, Lachlan Deck wrote:
>>
>>> Perhaps it'd make sense to be able to register a delegate... then  
>>> you can still separate concerns ;-)
>>
>> Could you elaborate some more?
>>
>> Most often than not I find the EOF-like delegate approach to be  
>> rather inflexible
>
> In what sense? I find it's a good fit when you need to insert some  
> shared logic within the call logic. However...
>
>> , and certainly not suitable for the framework internal needs.
>
> The other option presented in EOF which is also useful is that of  
> notifications. The difference between the two is usually that the  
> delegate can abort an operation or provide custom shared behaviour  
> whereas numerous objects can register as notification observers to  
> run their own logic based on the transpired events. So perhaps  
> notifications could be a good choice as any object can register  
> themselves as an observer of a notification, registering the method  
> to call when the notification is fired, and whether to limit the  
> notification to those fired from a particular object or for any for  
> that key. This seems to me to suit the lifecycle events nicely.
>
> Not only do individual PersistentObjects need to be notified of  
> prePersist/postPersist etc but I find it useful (in the EOF space)  
> of receiving a notification, when an editing context has been  
> saved, that provides with the notification the list of objects  
> deleted, inserted, and updated so as to deal with them as a group  
> (which could handle fulfilling the former by interating through the  
> objects can calling pre*/post*). But being able to register any  
> object against these various lifecycle events ought to provide for  
> additional flexibility.
>
> Btw, I'm thinking that PrePersist ought to be run just prior to  
> persisting an object (as the name suggests) and that another  
> postCreate lifecycle event ought to take its place. What do you  
> think? That seems to be the missing piece in the puzzle between  
> validateFor* and committing a new object.
>
> with regards,
> --
>
> Lachlan Deck


Re: weird problems with context

Posted by Lachlan Deck <la...@gmail.com>.
On 05/06/2007, at 10:31 PM, Andrus Adamchik wrote:

> On Jun 5, 2007, at 3:22 PM, Lachlan Deck wrote:
>
>> Perhaps it'd make sense to be able to register a delegate... then  
>> you can still separate concerns ;-)
>
> Could you elaborate some more?
>
> Most often than not I find the EOF-like delegate approach to be  
> rather inflexible

In what sense? I find it's a good fit when you need to insert some  
shared logic within the call logic. However...

> , and certainly not suitable for the framework internal needs.

The other option presented in EOF which is also useful is that of  
notifications. The difference between the two is usually that the  
delegate can abort an operation or provide custom shared behaviour  
whereas numerous objects can register as notification observers to  
run their own logic based on the transpired events. So perhaps  
notifications could be a good choice as any object can register  
themselves as an observer of a notification, registering the method  
to call when the notification is fired, and whether to limit the  
notification to those fired from a particular object or for any for  
that key. This seems to me to suit the lifecycle events nicely.

Not only do individual PersistentObjects need to be notified of  
prePersist/postPersist etc but I find it useful (in the EOF space) of  
receiving a notification, when an editing context has been saved,  
that provides with the notification the list of objects deleted,  
inserted, and updated so as to deal with them as a group (which could  
handle fulfilling the former by interating through the objects can  
calling pre*/post*). But being able to register any object against  
these various lifecycle events ought to provide for additional  
flexibility.

Btw, I'm thinking that PrePersist ought to be run just prior to  
persisting an object (as the name suggests) and that another  
postCreate lifecycle event ought to take its place. What do you  
think? That seems to be the missing piece in the puzzle between  
validateFor* and committing a new object.

with regards,
--

Lachlan Deck



Re: weird problems with context

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Jun 5, 2007, at 3:22 PM, Lachlan Deck wrote:

> Perhaps it'd make sense to be able to register a delegate... then  
> you can still separate concerns ;-)

Could you elaborate some more?

Most often than not I find the EOF-like delegate approach to be  
rather inflexible, and certainly not suitable for the framework  
internal needs.

Andrus

Re: weird problems with context

Posted by Lachlan Deck <la...@gmail.com>.
On 05/06/2007, at 9:29 PM, Andrus Adamchik wrote:

> This seems related to this thread started by Lachlan:
>
> http://objectstyle.org/cayenne/lists/cayenne-devel/2007/06/0027.html

It is.

> The use of a proxy context may confuse the issues. If you are aware  
> of this behavior, it is usually easy to work around (by avoiding  
> calls like "cat.getObjectContext().newObject(SomeEntity.class)").  
> We will try to fix it per CAY-797 though, by avoiding the use of  
> proxy context for callbacks (no matter how much I like the  
> separation of concerns and runtime flexibility provided by the  
> proxy-based design :-)).

Perhaps it'd make sense to be able to register a delegate... then you  
can still separate concerns ;-)

with regards,
--

Lachlan Deck




Re: weird problems with context

Posted by Andrus Adamchik <an...@objectstyle.org>.
This seems related to this thread started by Lachlan:

http://objectstyle.org/cayenne/lists/cayenne-devel/2007/06/0027.html

The use of a proxy context may confuse the issues. If you are aware  
of this behavior, it is usually easy to work around (by avoiding  
calls like "cat.getObjectContext().newObject(SomeEntity.class)"). We  
will try to fix it per CAY-797 though, by avoiding the use of proxy  
context for callbacks (no matter how much I like the separation of  
concerns and runtime flexibility provided by the proxy-based  
design :-)).

Andrus


On Jun 5, 2007, at 8:38 AM, Marcin Skladaniec wrote:

> Hi,
>
> I would like to clarify, that the context are not equal, and it  
> does not matter if I choose to compare them by "equal()" or "==".
>
> I cant still get to the bottom of it, how it is possible, that if  
> both listeners (prePersist and postPersist) are registered  
> together, only one of them is fired when using
> anObject.getObjectContext().newObject().
>
> Marcin
>
>
> On 02/06/2007, at 11:43 AM, Marcin Skladaniec wrote:
>
>> Hi
>> I'm having a strange problem. When executing this bit of code:
>>
>> List categories = aContext.performQuery(new SelectQuery 
>> (Category.class));
>> if (categories.size() != 0) {
>> 	for (int categoriesCount = 0; categoriesCount < categories.size 
>> (); categoriesCount++) {
>> 	 	Category cat = (Category) categories.get(categoriesCount);
>> 		logger.debug("context equal ? " + cat.getObjectContext().equals 
>> (aContext));
>> 	}
>> }
>> 							
>> the contexts are not equal, and I cant find the reason why !
>>
>> I found this problem, because the perPersist() callback is not  
>> fired when I do
>> cat.getObjectContext().newObject(SomeEntity.class)
>> but it is when
>> aContext.newObject(SomeEntity.class)
>> to add complexity, postPersist() and other callbacks are fired in  
>> both cases.
>>
>> Any clues ?
>>
>> Marcin
>>
>
>
>
>


Re: weird problems with context

Posted by Marcin Skladaniec <ma...@ish.com.au>.
Hi,

I would like to clarify, that the context are not equal, and it does  
not matter if I choose to compare them by "equal()" or "==".

I cant still get to the bottom of it, how it is possible, that if  
both listeners (prePersist and postPersist) are registered together,  
only one of them is fired when using
anObject.getObjectContext().newObject().

Marcin


On 02/06/2007, at 11:43 AM, Marcin Skladaniec wrote:

> Hi
> I'm having a strange problem. When executing this bit of code:
>
> List categories = aContext.performQuery(new SelectQuery 
> (Category.class));
> if (categories.size() != 0) {
> 	for (int categoriesCount = 0; categoriesCount < categories.size();  
> categoriesCount++) {
> 	 	Category cat = (Category) categories.get(categoriesCount);
> 		logger.debug("context equal ? " + cat.getObjectContext().equals 
> (aContext));
> 	}
> }
> 							
> the contexts are not equal, and I cant find the reason why !
>
> I found this problem, because the perPersist() callback is not  
> fired when I do
> cat.getObjectContext().newObject(SomeEntity.class)
> but it is when
> aContext.newObject(SomeEntity.class)
> to add complexity, postPersist() and other callbacks are fired in  
> both cases.
>
> Any clues ?
>
> Marcin
>