You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@godurkodi.is> on 2017/03/01 11:18:14 UTC

Validation and @PrePersist

Hi all,
I have some logic in a Listener that uses @PrePersist to populate the value of a required attribute before committing changes. Turns out this doesn’t work, since Cayenne invokes validateForInsert() before running @PrePersist.

Any suggestions for where I can invoke logic populates required values before validation?

Cheers,
- hugi

Re: Validation and @PrePersist

Posted by Michael Gentry <bl...@gmail.com>.
Hi Maik,

I've historically done data manipulations in the lifecycle events
(pre-persist, post-add, post-load, etc) and not the validate methods.  I
kind of agree with you that validate shouldn't be modifying data.  I'm
hoping someone else explains the change now.  :-)

mrg


On Wed, Mar 1, 2017 at 11:43 AM, Musall, Maik <ma...@selbstdenker.ag> wrote:

> Hi all,
>
> but isn't hte purpose of validation to check if the object to be saved is
> in a valid state? That would imply that validation has a positive (pass) or
> negative (ValidationException) result, and no side effects. Modifying the
> object during validation would defeat the purpose, wouldn't it?
>
> Maik
>
> > Am 01.03.2017 um 17:12 schrieb Matt Watson <ma...@swarmbox.com>:
> >
> > When I ran into this same scenario I realized that “validateForInsert”
> was the hook I needed, instead of @PrePersist.
> >
> > @Override
> > public void validateForInsert(ValidationResult validationResult) {
> >       if (getReference() == null || getReference().isEmpty()) {
> >               setReference(System.getNextPurchaseOrderReference());
> >       }
> >       super.validateForInsert(validationResult);
> > }
> >
> >
> >
> >> On Mar 1, 2017, at 5:51 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
> >>
> >> Hi Jurgen,
> >> fine suggestion but unfortunately not the part of the lifecycle I need
> to catch—the action needs to be performed before committing, not after
> adding (so basically I need @PrePersist—but I need it before validation
> happens).
> >>
> >> Cheers,
> >> - hugi
> >>
> >>
> >>> On 1. mar. 2017, at 13:14, <do...@xsinet.co.za> <do...@xsinet.co.za>
> wrote:
> >>>
> >>> Hi Hugi
> >>>
> >>> For this kind of thing use @PostAdd instead.
> >>>
> >>> Regards
> >>> Jurgen
> >>>
> >>>
> >>> -----Original Message----- From: Hugi Thordarson
> >>> Sent: Wednesday, March 1, 2017 1:18 PM
> >>> To: user@cayenne.apache.org
> >>> Subject: Validation and @PrePersist
> >>>
> >>> Hi all,
> >>> I have some logic in a Listener that uses @PrePersist to populate the
> value of a required attribute before committing changes. Turns out this
> doesn’t work, since Cayenne invokes validateForInsert() before running
> @PrePersist.
> >>>
> >>> Any suggestions for where I can invoke logic populates required values
> before validation?
> >>>
> >>> Cheers,
> >>> - hugi
> >>
> >
>
>

Re: Validation and @PrePersist

Posted by "Musall, Maik" <ma...@selbstdenker.ag>.
Hi all,

but isn't hte purpose of validation to check if the object to be saved is in a valid state? That would imply that validation has a positive (pass) or negative (ValidationException) result, and no side effects. Modifying the object during validation would defeat the purpose, wouldn't it?

Maik

> Am 01.03.2017 um 17:12 schrieb Matt Watson <ma...@swarmbox.com>:
> 
> When I ran into this same scenario I realized that “validateForInsert” was the hook I needed, instead of @PrePersist.
> 
> @Override
> public void validateForInsert(ValidationResult validationResult) {
> 	if (getReference() == null || getReference().isEmpty()) {
> 		setReference(System.getNextPurchaseOrderReference());
> 	}
> 	super.validateForInsert(validationResult);
> }
> 
> 
> 
>> On Mar 1, 2017, at 5:51 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>> 
>> Hi Jurgen,
>> fine suggestion but unfortunately not the part of the lifecycle I need to catch—the action needs to be performed before committing, not after adding (so basically I need @PrePersist—but I need it before validation happens).
>> 
>> Cheers,
>> - hugi
>> 
>> 
>>> On 1. mar. 2017, at 13:14, <do...@xsinet.co.za> <do...@xsinet.co.za> wrote:
>>> 
>>> Hi Hugi
>>> 
>>> For this kind of thing use @PostAdd instead.
>>> 
>>> Regards
>>> Jurgen
>>> 
>>> 
>>> -----Original Message----- From: Hugi Thordarson
>>> Sent: Wednesday, March 1, 2017 1:18 PM
>>> To: user@cayenne.apache.org
>>> Subject: Validation and @PrePersist
>>> 
>>> Hi all,
>>> I have some logic in a Listener that uses @PrePersist to populate the value of a required attribute before committing changes. Turns out this doesn’t work, since Cayenne invokes validateForInsert() before running @PrePersist.
>>> 
>>> Any suggestions for where I can invoke logic populates required values before validation?
>>> 
>>> Cheers,
>>> - hugi 
>> 
> 


Re: Validation and @PrePersist

Posted by Matt Watson <ma...@swarmbox.com>.
When I ran into this same scenario I realized that “validateForInsert” was the hook I needed, instead of @PrePersist.

@Override
public void validateForInsert(ValidationResult validationResult) {
	if (getReference() == null || getReference().isEmpty()) {
		setReference(System.getNextPurchaseOrderReference());
	}
	super.validateForInsert(validationResult);
}



> On Mar 1, 2017, at 5:51 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
> 
> Hi Jurgen,
> fine suggestion but unfortunately not the part of the lifecycle I need to catch—the action needs to be performed before committing, not after adding (so basically I need @PrePersist—but I need it before validation happens).
> 
> Cheers,
> - hugi
> 
> 
>> On 1. mar. 2017, at 13:14, <do...@xsinet.co.za> <do...@xsinet.co.za> wrote:
>> 
>> Hi Hugi
>> 
>> For this kind of thing use @PostAdd instead.
>> 
>> Regards
>> Jurgen
>> 
>> 
>> -----Original Message----- From: Hugi Thordarson
>> Sent: Wednesday, March 1, 2017 1:18 PM
>> To: user@cayenne.apache.org
>> Subject: Validation and @PrePersist
>> 
>> Hi all,
>> I have some logic in a Listener that uses @PrePersist to populate the value of a required attribute before committing changes. Turns out this doesn’t work, since Cayenne invokes validateForInsert() before running @PrePersist.
>> 
>> Any suggestions for where I can invoke logic populates required values before validation?
>> 
>> Cheers,
>> - hugi 
> 


Re: Validation and @PrePersist

Posted by Adam Boyle <ab...@valsphere.com>.
I'd like to second this suggestion. There have been several instances that I've made required fields nullable solely because validation occurs prior to the @PrePersist lifecycle callback.

________________________________
From: Hugi Thordarson <hu...@godurkodi.is>
Sent: Wednesday, March 1, 2017 8:51:48 AM
To: user@cayenne.apache.org
Subject: Re: Validation and @PrePersist

Hi Jurgen,
fine suggestion but unfortunately not the part of the lifecycle I need to catch—the action needs to be performed before committing, not after adding (so basically I need @PrePersist—but I need it before validation happens).

Cheers,
- hugi


> On 1. mar. 2017, at 13:14, <do...@xsinet.co.za> <do...@xsinet.co.za> wrote:
>
> Hi Hugi
>
> For this kind of thing use @PostAdd instead.
>
> Regards
> Jurgen
>
>
> -----Original Message----- From: Hugi Thordarson
> Sent: Wednesday, March 1, 2017 1:18 PM
> To: user@cayenne.apache.org
> Subject: Validation and @PrePersist
>
> Hi all,
> I have some logic in a Listener that uses @PrePersist to populate the value of a required attribute before committing changes. Turns out this doesn’t work, since Cayenne invokes validateForInsert() before running @PrePersist.
>
> Any suggestions for where I can invoke logic populates required values before validation?
>
> Cheers,
> - hugi


Re: Validation and @PrePersist

Posted by Hugi Thordarson <hu...@godurkodi.is>.
Hi Jurgen,
fine suggestion but unfortunately not the part of the lifecycle I need to catch—the action needs to be performed before committing, not after adding (so basically I need @PrePersist—but I need it before validation happens).

Cheers,
- hugi


> On 1. mar. 2017, at 13:14, <do...@xsinet.co.za> <do...@xsinet.co.za> wrote:
> 
> Hi Hugi
> 
> For this kind of thing use @PostAdd instead.
> 
> Regards
> Jurgen
> 
> 
> -----Original Message----- From: Hugi Thordarson
> Sent: Wednesday, March 1, 2017 1:18 PM
> To: user@cayenne.apache.org
> Subject: Validation and @PrePersist
> 
> Hi all,
> I have some logic in a Listener that uses @PrePersist to populate the value of a required attribute before committing changes. Turns out this doesn’t work, since Cayenne invokes validateForInsert() before running @PrePersist.
> 
> Any suggestions for where I can invoke logic populates required values before validation?
> 
> Cheers,
> - hugi 


Re: Validation and @PrePersist

Posted by do...@xsinet.co.za.
Hi Hugi

For this kind of thing use @PostAdd instead.

Regards
Jurgen


-----Original Message----- 
From: Hugi Thordarson
Sent: Wednesday, March 1, 2017 1:18 PM
To: user@cayenne.apache.org
Subject: Validation and @PrePersist

Hi all,
I have some logic in a Listener that uses @PrePersist to populate the value 
of a required attribute before committing changes. Turns out this doesn\u2019t 
work, since Cayenne invokes validateForInsert() before running @PrePersist.

Any suggestions for where I can invoke logic populates required values 
before validation?

Cheers,
- hugi 


Re: Validation and @PrePersist

Posted by "Musall, Maik" <ma...@selbstdenker.ag>.
Hi Nikita,

thanks for completing the investigation on this. I have workarounds in place and am totally ok to have this tackled not before 4.1.

Maik


> Am 04.05.2017 um 11:39 schrieb Nikita Timofeev <nt...@objectstyle.com>:
> 
> Hi all,
> 
> I've dived into this problem and it seems that there is
> no simple (and more importantly safe) solution
> and I propose to postpone this change until 4.1.
> 
> Here is some details for the curious ones :)
> 
> Currently validation is done by DataContext (either when flushing
> changes to DataDomain or to parent context) while callbacks
> are performed inside DataDomain's filters (namely inside TransactionFilter).
> And just moving those parts into one place produce
> a lot of possible incompatibilities:
> 1. When pushing validation down to filters we still need it to be performed
> when flushing to parent context and moreover validation relies on GraphDiff
> implementation that is unknown to filters.
> 2. When pulling callbacks to DataContext filters that rely on them
> will stop working (like CacheInvalidationFilter or old AuditableFilter).
> 
> On Thu, Apr 6, 2017 at 3:05 PM, Nikita Timofeev
> <nt...@objectstyle.com> wrote:
>> Hi Maik,
>> 
>> This bug affected only the case when LifecycleListener interface where
>> explicitly used (in method addListener(Class, LifecycleListener)),
>> not the case when custom listeners with annotated methods are used.
>> As I understand this is not a very popular usage pattern.
>> 
>> On Thu, Apr 6, 2017 at 2:37 PM, Musall, Maik <ma...@selbstdenker.ag> wrote:
>>> Hi again,
>>> 
>>> could this just have been this bug that Nikita fixed yesterday?
>>> 
>>> https://issues.apache.org/jira/browse/CAY-2276 <https://issues.apache.org/jira/browse/CAY-2276>
>>> 
>>> Maik
>>> 
>>> 
>>>> Am 03.03.2017 um 17:48 schrieb Andrus Adamchik <an...@objectstyle.org>:
>>>> 
>>>> Yeah, we'd need to do some forensics on this. I no longer remember how this change occurred. Could've been a bug that got legitimized via CAY-2039 ... or not. Also, to add some context to this, we wanted to merge validation and callback mechanisms in one. This got excluded from 4.0 scope though. Perhaps past 4.0 we'll come up with some universal callback mechanism, free of JPA heritage.
>>>> 
>>>> Andrus
>>>> 
>>>>> On Mar 3, 2017, at 5:13 PM, dollj@xsinet.co.za wrote:
>>>>> 
>>>>> Created JIRA CAY-2254
>>>>> 
>>>>> 
>>>>> -----Original Message-----
>>>>> From: Musall, Maik
>>>>> Sent: Thursday, March 2, 2017 10:49 AM
>>>>> To: user@cayenne.apache.org
>>>>> Subject: Re: Validation and @PrePersist
>>>>> 
>>>>> Hi Jurgen,
>>>>> 
>>>>>> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
>>>>>> 
>>>>>> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?
>>>>> 
>>>>> That would be my conclusion, too.
>>>>> 
>>>>>> It would be good for this to be changed, unless there's backward compatibility issues  :-)
>>>>> 
>>>>> Well, there's no better time to change this than at the beginning of M6, right? :-)
>>>>> 
>>>>> Maik
>>>>> 
>>>>> 
>>>>>> 
>>>>>> -----Original Message----- From: Musall, Maik
>>>>>> Sent: Thursday, March 2, 2017 9:53 AM
>>>>>> To: user@cayenne.apache.org
>>>>>> Subject: Re: Validation and @PrePersist
>>>>>> 
>>>>>> Hi Jurgen,
>>>>>> 
>>>>>> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
>>>>>> 
>>>>>> 1. validation is supposed to not change attributes
>>>>>> 2. no more changes should be made after validation happened
>>>>>> 3. PrePersist runs after validation
>>>>>> 
>>>>>> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
>>>>>> 
>>>>>> Maik
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>>>>>>> 
>>>>>>> Hi All
>>>>>>> 
>>>>>>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>>>>>>> 
>>>>>>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>>>>>>> 
>>>>>>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>>>>>>> 
>>>>>>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>>>>>>> 
>>>>>>> Cheers,
>>>>>>> Jurgen
>>>>>>> 
>>>>>>> 
>>>>>>> -----Original Message----- From: Michael Gentry
>>>>>>> Sent: Wednesday, March 1, 2017 5:38 PM
>>>>>>> To: Cayenne Users
>>>>>>> Subject: Re: Validation and @PrePersist
>>>>>>> 
>>>>>>> Hi Hugi,
>>>>>>> 
>>>>>>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>>>>>>> someone else can answer that one...
>>>>>>> 
>>>>>>> Thanks,
>>>>>>> 
>>>>>>> mrg
>>>>>>> 
>>>>>>> 
>>>>>>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>>>>>>> 
>>>>>>>> Hi Michael,
>>>>>>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>>>>>>> current behaviour:
>>>>>>>> 
>>>>>>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>>>>>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>>>>>>> 
>>>>>>>> Cheers,
>>>>>>>> - hugi
>>>>>>>> 
>>>>>>>> 
>>>>>>>>> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>>>>>>>> 
>>>>>>>>> Hi Hugi,
>>>>>>>>> 
>>>>>>>>> If validateForInsert() is being called before PrePersist, we have a
>>>>>>>>> disconnect between the documentation and the behavior you are seeing:
>>>>>>>>> 
>>>>>>>>> "PrePersist: right before a new object is committed, inside
>>>>>>>>> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>>>>>>>> (and prior to validateForInsert())."
>>>>>>>>> 
>>>>>>>>> What version of Cayenne are you using?  I wonder if something has > > changed
>>>>>>>>> or if the documentation is just wrong.
>>>>>>>>> 
>>>>>>>>> Thanks,
>>>>>>>>> 
>>>>>>>>> mrg
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>>>>>>> wrote:
>>>>>>>>> 
>>>>>>>>>> Hi all,
>>>>>>>>>> I have some logic in a Listener that uses @PrePersist to populate the
>>>>>>>>>> value of a required attribute before committing changes. Turns out >> this
>>>>>>>>>> doesn’t work, since Cayenne invokes validateForInsert() before running
>>>>>>>>>> @PrePersist.
>>>>>>>>>> 
>>>>>>>>>> Any suggestions for where I can invoke logic populates required values
>>>>>>>>>> before validation?
>>>>>>>>>> 
>>>>>>>>>> Cheers,
>>>>>>>>>> - hugi
>>>>>>>> 
>>>>>>> 
>>>>>> 
>>>> 
>>> 
>> 
>> 
>> 
>> --
>> Best regards,
>> Nikita Timofeev
> 
> 
> 
> -- 
> Best regards,
> Nikita Timofeev


Re: Validation and @PrePersist

Posted by Nikita Timofeev <nt...@objectstyle.com>.
Hi all,

I've dived into this problem and it seems that there is
no simple (and more importantly safe) solution
and I propose to postpone this change until 4.1.

Here is some details for the curious ones :)

Currently validation is done by DataContext (either when flushing
changes to DataDomain or to parent context) while callbacks
are performed inside DataDomain's filters (namely inside TransactionFilter).
And just moving those parts into one place produce
a lot of possible incompatibilities:
1. When pushing validation down to filters we still need it to be performed
when flushing to parent context and moreover validation relies on GraphDiff
implementation that is unknown to filters.
2. When pulling callbacks to DataContext filters that rely on them
will stop working (like CacheInvalidationFilter or old AuditableFilter).

On Thu, Apr 6, 2017 at 3:05 PM, Nikita Timofeev
<nt...@objectstyle.com> wrote:
> Hi Maik,
>
> This bug affected only the case when LifecycleListener interface where
> explicitly used (in method addListener(Class, LifecycleListener)),
> not the case when custom listeners with annotated methods are used.
> As I understand this is not a very popular usage pattern.
>
> On Thu, Apr 6, 2017 at 2:37 PM, Musall, Maik <ma...@selbstdenker.ag> wrote:
>> Hi again,
>>
>> could this just have been this bug that Nikita fixed yesterday?
>>
>> https://issues.apache.org/jira/browse/CAY-2276 <https://issues.apache.org/jira/browse/CAY-2276>
>>
>> Maik
>>
>>
>>> Am 03.03.2017 um 17:48 schrieb Andrus Adamchik <an...@objectstyle.org>:
>>>
>>> Yeah, we'd need to do some forensics on this. I no longer remember how this change occurred. Could've been a bug that got legitimized via CAY-2039 ... or not. Also, to add some context to this, we wanted to merge validation and callback mechanisms in one. This got excluded from 4.0 scope though. Perhaps past 4.0 we'll come up with some universal callback mechanism, free of JPA heritage.
>>>
>>> Andrus
>>>
>>>> On Mar 3, 2017, at 5:13 PM, dollj@xsinet.co.za wrote:
>>>>
>>>> Created JIRA CAY-2254
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: Musall, Maik
>>>> Sent: Thursday, March 2, 2017 10:49 AM
>>>> To: user@cayenne.apache.org
>>>> Subject: Re: Validation and @PrePersist
>>>>
>>>> Hi Jurgen,
>>>>
>>>>> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
>>>>>
>>>>> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?
>>>>
>>>> That would be my conclusion, too.
>>>>
>>>>> It would be good for this to be changed, unless there's backward compatibility issues  :-)
>>>>
>>>> Well, there's no better time to change this than at the beginning of M6, right? :-)
>>>>
>>>> Maik
>>>>
>>>>
>>>>>
>>>>> -----Original Message----- From: Musall, Maik
>>>>> Sent: Thursday, March 2, 2017 9:53 AM
>>>>> To: user@cayenne.apache.org
>>>>> Subject: Re: Validation and @PrePersist
>>>>>
>>>>> Hi Jurgen,
>>>>>
>>>>> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
>>>>>
>>>>> 1. validation is supposed to not change attributes
>>>>> 2. no more changes should be made after validation happened
>>>>> 3. PrePersist runs after validation
>>>>>
>>>>> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
>>>>>
>>>>> Maik
>>>>>
>>>>>
>>>>>
>>>>>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>>>>>>
>>>>>> Hi All
>>>>>>
>>>>>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>>>>>>
>>>>>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>>>>>>
>>>>>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>>>>>>
>>>>>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>>>>>>
>>>>>> Cheers,
>>>>>> Jurgen
>>>>>>
>>>>>>
>>>>>> -----Original Message----- From: Michael Gentry
>>>>>> Sent: Wednesday, March 1, 2017 5:38 PM
>>>>>> To: Cayenne Users
>>>>>> Subject: Re: Validation and @PrePersist
>>>>>>
>>>>>> Hi Hugi,
>>>>>>
>>>>>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>>>>>> someone else can answer that one...
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> mrg
>>>>>>
>>>>>>
>>>>>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>>>>>>
>>>>>>> Hi Michael,
>>>>>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>>>>>> current behaviour:
>>>>>>>
>>>>>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>>>>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>>>>>>
>>>>>>> Cheers,
>>>>>>> - hugi
>>>>>>>
>>>>>>>
>>>>>>>> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>>>>>>>
>>>>>>>> Hi Hugi,
>>>>>>>>
>>>>>>>> If validateForInsert() is being called before PrePersist, we have a
>>>>>>>> disconnect between the documentation and the behavior you are seeing:
>>>>>>>>
>>>>>>>> "PrePersist: right before a new object is committed, inside
>>>>>>>> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>>>>>>> (and prior to validateForInsert())."
>>>>>>>>
>>>>>>>> What version of Cayenne are you using?  I wonder if something has > > changed
>>>>>>>> or if the documentation is just wrong.
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>>
>>>>>>>> mrg
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Hi all,
>>>>>>>>> I have some logic in a Listener that uses @PrePersist to populate the
>>>>>>>>> value of a required attribute before committing changes. Turns out >> this
>>>>>>>>> doesn’t work, since Cayenne invokes validateForInsert() before running
>>>>>>>>> @PrePersist.
>>>>>>>>>
>>>>>>>>> Any suggestions for where I can invoke logic populates required values
>>>>>>>>> before validation?
>>>>>>>>>
>>>>>>>>> Cheers,
>>>>>>>>> - hugi
>>>>>>>
>>>>>>
>>>>>
>>>
>>
>
>
>
> --
> Best regards,
> Nikita Timofeev



-- 
Best regards,
Nikita Timofeev

Re: Validation and @PrePersist

Posted by Nikita Timofeev <nt...@objectstyle.com>.
Hi Maik,

This bug affected only the case when LifecycleListener interface where
explicitly used (in method addListener(Class, LifecycleListener)),
not the case when custom listeners with annotated methods are used.
As I understand this is not a very popular usage pattern.

On Thu, Apr 6, 2017 at 2:37 PM, Musall, Maik <ma...@selbstdenker.ag> wrote:
> Hi again,
>
> could this just have been this bug that Nikita fixed yesterday?
>
> https://issues.apache.org/jira/browse/CAY-2276 <https://issues.apache.org/jira/browse/CAY-2276>
>
> Maik
>
>
>> Am 03.03.2017 um 17:48 schrieb Andrus Adamchik <an...@objectstyle.org>:
>>
>> Yeah, we'd need to do some forensics on this. I no longer remember how this change occurred. Could've been a bug that got legitimized via CAY-2039 ... or not. Also, to add some context to this, we wanted to merge validation and callback mechanisms in one. This got excluded from 4.0 scope though. Perhaps past 4.0 we'll come up with some universal callback mechanism, free of JPA heritage.
>>
>> Andrus
>>
>>> On Mar 3, 2017, at 5:13 PM, dollj@xsinet.co.za wrote:
>>>
>>> Created JIRA CAY-2254
>>>
>>>
>>> -----Original Message-----
>>> From: Musall, Maik
>>> Sent: Thursday, March 2, 2017 10:49 AM
>>> To: user@cayenne.apache.org
>>> Subject: Re: Validation and @PrePersist
>>>
>>> Hi Jurgen,
>>>
>>>> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
>>>>
>>>> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?
>>>
>>> That would be my conclusion, too.
>>>
>>>> It would be good for this to be changed, unless there's backward compatibility issues  :-)
>>>
>>> Well, there's no better time to change this than at the beginning of M6, right? :-)
>>>
>>> Maik
>>>
>>>
>>>>
>>>> -----Original Message----- From: Musall, Maik
>>>> Sent: Thursday, March 2, 2017 9:53 AM
>>>> To: user@cayenne.apache.org
>>>> Subject: Re: Validation and @PrePersist
>>>>
>>>> Hi Jurgen,
>>>>
>>>> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
>>>>
>>>> 1. validation is supposed to not change attributes
>>>> 2. no more changes should be made after validation happened
>>>> 3. PrePersist runs after validation
>>>>
>>>> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
>>>>
>>>> Maik
>>>>
>>>>
>>>>
>>>>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>>>>>
>>>>> Hi All
>>>>>
>>>>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>>>>>
>>>>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>>>>>
>>>>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>>>>>
>>>>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>>>>>
>>>>> Cheers,
>>>>> Jurgen
>>>>>
>>>>>
>>>>> -----Original Message----- From: Michael Gentry
>>>>> Sent: Wednesday, March 1, 2017 5:38 PM
>>>>> To: Cayenne Users
>>>>> Subject: Re: Validation and @PrePersist
>>>>>
>>>>> Hi Hugi,
>>>>>
>>>>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>>>>> someone else can answer that one...
>>>>>
>>>>> Thanks,
>>>>>
>>>>> mrg
>>>>>
>>>>>
>>>>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>>>>>
>>>>>> Hi Michael,
>>>>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>>>>> current behaviour:
>>>>>>
>>>>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>>>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>>>>>
>>>>>> Cheers,
>>>>>> - hugi
>>>>>>
>>>>>>
>>>>>>> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>>>>>>
>>>>>>> Hi Hugi,
>>>>>>>
>>>>>>> If validateForInsert() is being called before PrePersist, we have a
>>>>>>> disconnect between the documentation and the behavior you are seeing:
>>>>>>>
>>>>>>> "PrePersist: right before a new object is committed, inside
>>>>>>> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>>>>>> (and prior to validateForInsert())."
>>>>>>>
>>>>>>> What version of Cayenne are you using?  I wonder if something has > > changed
>>>>>>> or if the documentation is just wrong.
>>>>>>>
>>>>>>> Thanks,
>>>>>>>
>>>>>>> mrg
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>>>>> wrote:
>>>>>>>
>>>>>>>> Hi all,
>>>>>>>> I have some logic in a Listener that uses @PrePersist to populate the
>>>>>>>> value of a required attribute before committing changes. Turns out >> this
>>>>>>>> doesn’t work, since Cayenne invokes validateForInsert() before running
>>>>>>>> @PrePersist.
>>>>>>>>
>>>>>>>> Any suggestions for where I can invoke logic populates required values
>>>>>>>> before validation?
>>>>>>>>
>>>>>>>> Cheers,
>>>>>>>> - hugi
>>>>>>
>>>>>
>>>>
>>
>



-- 
Best regards,
Nikita Timofeev

Re: Validation and @PrePersist

Posted by "Musall, Maik" <ma...@selbstdenker.ag>.
Hi again,

could this just have been this bug that Nikita fixed yesterday?

https://issues.apache.org/jira/browse/CAY-2276 <https://issues.apache.org/jira/browse/CAY-2276>

Maik


> Am 03.03.2017 um 17:48 schrieb Andrus Adamchik <an...@objectstyle.org>:
> 
> Yeah, we'd need to do some forensics on this. I no longer remember how this change occurred. Could've been a bug that got legitimized via CAY-2039 ... or not. Also, to add some context to this, we wanted to merge validation and callback mechanisms in one. This got excluded from 4.0 scope though. Perhaps past 4.0 we'll come up with some universal callback mechanism, free of JPA heritage.
> 
> Andrus
> 
>> On Mar 3, 2017, at 5:13 PM, dollj@xsinet.co.za wrote:
>> 
>> Created JIRA CAY-2254
>> 
>> 
>> -----Original Message----- 
>> From: Musall, Maik 
>> Sent: Thursday, March 2, 2017 10:49 AM 
>> To: user@cayenne.apache.org 
>> Subject: Re: Validation and @PrePersist 
>> 
>> Hi Jurgen,
>> 
>>> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
>>> 
>>> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?
>> 
>> That would be my conclusion, too.
>> 
>>> It would be good for this to be changed, unless there's backward compatibility issues  :-)
>> 
>> Well, there's no better time to change this than at the beginning of M6, right? :-)
>> 
>> Maik
>> 
>> 
>>> 
>>> -----Original Message----- From: Musall, Maik
>>> Sent: Thursday, March 2, 2017 9:53 AM
>>> To: user@cayenne.apache.org
>>> Subject: Re: Validation and @PrePersist
>>> 
>>> Hi Jurgen,
>>> 
>>> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
>>> 
>>> 1. validation is supposed to not change attributes
>>> 2. no more changes should be made after validation happened
>>> 3. PrePersist runs after validation
>>> 
>>> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
>>> 
>>> Maik
>>> 
>>> 
>>> 
>>>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>>>> 
>>>> Hi All
>>>> 
>>>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>>>> 
>>>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>>>> 
>>>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>>>> 
>>>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>>>> 
>>>> Cheers,
>>>> Jurgen
>>>> 
>>>> 
>>>> -----Original Message----- From: Michael Gentry
>>>> Sent: Wednesday, March 1, 2017 5:38 PM
>>>> To: Cayenne Users
>>>> Subject: Re: Validation and @PrePersist
>>>> 
>>>> Hi Hugi,
>>>> 
>>>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>>>> someone else can answer that one...
>>>> 
>>>> Thanks,
>>>> 
>>>> mrg
>>>> 
>>>> 
>>>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>>>> 
>>>>> Hi Michael,
>>>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>>>> current behaviour:
>>>>> 
>>>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>>>> 
>>>>> Cheers,
>>>>> - hugi
>>>>> 
>>>>> 
>>>>>> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>>>>> 
>>>>>> Hi Hugi,
>>>>>> 
>>>>>> If validateForInsert() is being called before PrePersist, we have a
>>>>>> disconnect between the documentation and the behavior you are seeing:
>>>>>> 
>>>>>> "PrePersist: right before a new object is committed, inside
>>>>>> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>>>>> (and prior to validateForInsert())."
>>>>>> 
>>>>>> What version of Cayenne are you using?  I wonder if something has > > changed
>>>>>> or if the documentation is just wrong.
>>>>>> 
>>>>>> Thanks,
>>>>>> 
>>>>>> mrg
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>>>> wrote:
>>>>>> 
>>>>>>> Hi all,
>>>>>>> I have some logic in a Listener that uses @PrePersist to populate the
>>>>>>> value of a required attribute before committing changes. Turns out >> this
>>>>>>> doesn’t work, since Cayenne invokes validateForInsert() before running
>>>>>>> @PrePersist.
>>>>>>> 
>>>>>>> Any suggestions for where I can invoke logic populates required values
>>>>>>> before validation?
>>>>>>> 
>>>>>>> Cheers,
>>>>>>> - hugi
>>>>> 
>>>> 
>>> 
> 


Re: Validation and @PrePersist

Posted by Andrus Adamchik <an...@objectstyle.org>.
Yeah, we'd need to do some forensics on this. I no longer remember how this change occurred. Could've been a bug that got legitimized via CAY-2039 ... or not. Also, to add some context to this, we wanted to merge validation and callback mechanisms in one. This got excluded from 4.0 scope though. Perhaps past 4.0 we'll come up with some universal callback mechanism, free of JPA heritage.

Andrus

> On Mar 3, 2017, at 5:13 PM, dollj@xsinet.co.za wrote:
> 
> Created JIRA CAY-2254
> 
> 
> -----Original Message----- 
> From: Musall, Maik 
> Sent: Thursday, March 2, 2017 10:49 AM 
> To: user@cayenne.apache.org 
> Subject: Re: Validation and @PrePersist 
> 
> Hi Jurgen,
> 
>> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
>> 
>> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?
> 
> That would be my conclusion, too.
> 
>> It would be good for this to be changed, unless there's backward compatibility issues  :-)
> 
> Well, there's no better time to change this than at the beginning of M6, right? :-)
> 
> Maik
> 
> 
>> 
>> -----Original Message----- From: Musall, Maik
>> Sent: Thursday, March 2, 2017 9:53 AM
>> To: user@cayenne.apache.org
>> Subject: Re: Validation and @PrePersist
>> 
>> Hi Jurgen,
>> 
>> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
>> 
>> 1. validation is supposed to not change attributes
>> 2. no more changes should be made after validation happened
>> 3. PrePersist runs after validation
>> 
>> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
>> 
>> Maik
>> 
>> 
>> 
>>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>>> 
>>> Hi All
>>> 
>>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>>> 
>>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>>> 
>>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>>> 
>>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>>> 
>>> Cheers,
>>> Jurgen
>>> 
>>> 
>>> -----Original Message----- From: Michael Gentry
>>> Sent: Wednesday, March 1, 2017 5:38 PM
>>> To: Cayenne Users
>>> Subject: Re: Validation and @PrePersist
>>> 
>>> Hi Hugi,
>>> 
>>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>>> someone else can answer that one...
>>> 
>>> Thanks,
>>> 
>>> mrg
>>> 
>>> 
>>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>>> 
>>>> Hi Michael,
>>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>>> current behaviour:
>>>> 
>>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>>> 
>>>> Cheers,
>>>> - hugi
>>>> 
>>>> 
>>>>> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>>>> 
>>>>> Hi Hugi,
>>>>> 
>>>>> If validateForInsert() is being called before PrePersist, we have a
>>>>> disconnect between the documentation and the behavior you are seeing:
>>>>> 
>>>>> "PrePersist: right before a new object is committed, inside
>>>>> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>>>> (and prior to validateForInsert())."
>>>>> 
>>>>> What version of Cayenne are you using?  I wonder if something has > > changed
>>>>> or if the documentation is just wrong.
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> mrg
>>>>> 
>>>>> 
>>>>> 
>>>>> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>>> wrote:
>>>>> 
>>>>>> Hi all,
>>>>>> I have some logic in a Listener that uses @PrePersist to populate the
>>>>>> value of a required attribute before committing changes. Turns out >> this
>>>>>> doesn’t work, since Cayenne invokes validateForInsert() before running
>>>>>> @PrePersist.
>>>>>> 
>>>>>> Any suggestions for where I can invoke logic populates required values
>>>>>> before validation?
>>>>>> 
>>>>>> Cheers,
>>>>>> - hugi
>>>> 
>>> 
>> 


Re: Validation and @PrePersist

Posted by do...@xsinet.co.za.
Created JIRA CAY-2254


-----Original Message----- 
From: Musall, Maik 
Sent: Thursday, March 2, 2017 10:49 AM 
To: user@cayenne.apache.org 
Subject: Re: Validation and @PrePersist 

Hi Jurgen,

> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
> 
> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?

That would be my conclusion, too.

> It would be good for this to be changed, unless there's backward compatibility issues  :-)

Well, there's no better time to change this than at the beginning of M6, right? :-)

Maik


> 
> -----Original Message----- From: Musall, Maik
> Sent: Thursday, March 2, 2017 9:53 AM
> To: user@cayenne.apache.org
> Subject: Re: Validation and @PrePersist
> 
> Hi Jurgen,
> 
> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
> 
> 1. validation is supposed to not change attributes
> 2. no more changes should be made after validation happened
> 3. PrePersist runs after validation
> 
> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
> 
> Maik
> 
> 
> 
>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>> 
>> Hi All
>> 
>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>> 
>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>> 
>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>> 
>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>> 
>> Cheers,
>> Jurgen
>> 
>> 
>> -----Original Message----- From: Michael Gentry
>> Sent: Wednesday, March 1, 2017 5:38 PM
>> To: Cayenne Users
>> Subject: Re: Validation and @PrePersist
>> 
>> Hi Hugi,
>> 
>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>> someone else can answer that one...
>> 
>> Thanks,
>> 
>> mrg
>> 
>> 
>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>> 
>>> Hi Michael,
>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>> current behaviour:
>>> 
>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>> 
>>> Cheers,
>>> - hugi
>>> 
>>> 
>>> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>> >
>>> > Hi Hugi,
>>> >
>>> > If validateForInsert() is being called before PrePersist, we have a
>>> > disconnect between the documentation and the behavior you are seeing:
>>> >
>>> > "PrePersist: right before a new object is committed, inside
>>> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>> > (and prior to validateForInsert())."
>>> >
>>> > What version of Cayenne are you using?  I wonder if something has > > changed
>>> > or if the documentation is just wrong.
>>> >
>>> > Thanks,
>>> >
>>> > mrg
>>> >
>>> >
>>> >
>>> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>> wrote:
>>> >
>>> >> Hi all,
>>> >> I have some logic in a Listener that uses @PrePersist to populate the
>>> >> value of a required attribute before committing changes. Turns out >> this
>>> >> doesn’t work, since Cayenne invokes validateForInsert() before running
>>> >> @PrePersist.
>>> >>
>>> >> Any suggestions for where I can invoke logic populates required values
>>> >> before validation?
>>> >>
>>> >> Cheers,
>>> >> - hugi
>>> 
>> 
> 

Re: Validation and @PrePersist

Posted by "Musall, Maik" <ma...@selbstdenker.ag>.
Hi Jurgen,

> Am 02.03.2017 um 09:33 schrieb dollj@xsinet.co.za:
> 
> I agree with your reasoning.  So then the original description of how lifecycle events should behave in 3.1 is conceptually better (i.e. that PrePersist & PreUpdate occur before validation), and the historical behaviour is then a bug ?

That would be my conclusion, too.

> It would be good for this to be changed, unless there's backward compatibility issues  :-)

Well, there's no better time to change this than at the beginning of M6, right? :-)

Maik


> 
> -----Original Message----- From: Musall, Maik
> Sent: Thursday, March 2, 2017 9:53 AM
> To: user@cayenne.apache.org
> Subject: Re: Validation and @PrePersist
> 
> Hi Jurgen,
> 
> PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:
> 
> 1. validation is supposed to not change attributes
> 2. no more changes should be made after validation happened
> 3. PrePersist runs after validation
> 
> then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.
> 
> Maik
> 
> 
> 
>> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>> 
>> Hi All
>> 
>> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
>> 
>> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
>> 
>> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
>> 
>> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
>> 
>> Cheers,
>> Jurgen
>> 
>> 
>> -----Original Message----- From: Michael Gentry
>> Sent: Wednesday, March 1, 2017 5:38 PM
>> To: Cayenne Users
>> Subject: Re: Validation and @PrePersist
>> 
>> Hi Hugi,
>> 
>> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
>> someone else can answer that one...
>> 
>> Thanks,
>> 
>> mrg
>> 
>> 
>> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
>> 
>>> Hi Michael,
>>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>>> current behaviour:
>>> 
>>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>> 
>>> Cheers,
>>> - hugi
>>> 
>>> 
>>> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>>> >
>>> > Hi Hugi,
>>> >
>>> > If validateForInsert() is being called before PrePersist, we have a
>>> > disconnect between the documentation and the behavior you are seeing:
>>> >
>>> > "PrePersist: right before a new object is committed, inside
>>> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>>> > (and prior to validateForInsert())."
>>> >
>>> > What version of Cayenne are you using?  I wonder if something has > > changed
>>> > or if the documentation is just wrong.
>>> >
>>> > Thanks,
>>> >
>>> > mrg
>>> >
>>> >
>>> >
>>> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>>> wrote:
>>> >
>>> >> Hi all,
>>> >> I have some logic in a Listener that uses @PrePersist to populate the
>>> >> value of a required attribute before committing changes. Turns out >> this
>>> >> doesn’t work, since Cayenne invokes validateForInsert() before running
>>> >> @PrePersist.
>>> >>
>>> >> Any suggestions for where I can invoke logic populates required values
>>> >> before validation?
>>> >>
>>> >> Cheers,
>>> >> - hugi
>>> 
>> 
> 


Re: Validation and @PrePersist

Posted by do...@xsinet.co.za.
Hi Maik

I agree with your reasoning.  So then the original description of how 
lifecycle events should behave in 3.1 is conceptually better (i.e. that 
PrePersist & PreUpdate occur before validation), and the historical 
behaviour is then a bug ?

It would be good for this to be changed, unless there's backward 
compatibility issues  :-)

Jurgen


-----Original Message----- 
From: Musall, Maik
Sent: Thursday, March 2, 2017 9:53 AM
To: user@cayenne.apache.org
Subject: Re: Validation and @PrePersist

Hi Jurgen,

PostAdd is fine for seting a createDate, but what about an updateDate or 
something similar? If these assertions are all true:

1. validation is supposed to not change attributes
2. no more changes should be made after validation happened
3. PrePersist runs after validation

then there is just no place left to set something like an updateDate. Also, 
PrePersist would be restricted to do read-only checks that you can also do 
during validation, which makes no sense to me conceptually.

Maik



> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
>
> Hi All
>
> We had a similar discussion Sept-Oct 2012 where it was noted that there 
> was a discrepancy between the docs and the implementation. That was with 
> 3.1B where the behaviour was the same as it is now, i.e. validation occurs 
> before PrePersist (but as noted by Michael the 3.1 docs incorrectly state 
> "after" ).
>
> So I assume that subsequently the docs were updated for cayenne 4 to 
> correctly reflect the behaviour.
>
> Back then it was stated that PostAdd was the correct place to do this kind 
> of thing, which I think is fine for fields that have standard default 
> values.
>
> It can get a bit clumsy sometimes where you sometimes have to have both 
> PostAdd and PrePersist doing the same thing. For example for a timestamp 
> field where object entity creation (PostAdd) occurs much earlier than the 
> commit (PrePersist), but you want the commit timestamp. This is where I 
> would have preferred the behaviour that Hugi is looking for where 
> PrePersist is called first, before validation.
>
> Cheers,
> Jurgen
>
>
> -----Original Message----- From: Michael Gentry
> Sent: Wednesday, March 1, 2017 5:38 PM
> To: Cayenne Users
> Subject: Re: Validation and @PrePersist
>
> Hi Hugi,
>
> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
> someone else can answer that one...
>
> Thanks,
>
> mrg
>
>
> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> 
> wrote:
>
>> Hi Michael,
>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>> current behaviour:
>>
>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>>
>> Cheers,
>> - hugi
>>
>>
>> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>> >
>> > Hi Hugi,
>> >
>> > If validateForInsert() is being called before PrePersist, we have a
>> > disconnect between the documentation and the behavior you are seeing:
>> >
>> > "PrePersist: right before a new object is committed, inside
>> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>> > (and prior to validateForInsert())."
>> >
>> > What version of Cayenne are you using?  I wonder if something has > 
>> > changed
>> > or if the documentation is just wrong.
>> >
>> > Thanks,
>> >
>> > mrg
>> >
>> >
>> >
>> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>> wrote:
>> >
>> >> Hi all,
>> >> I have some logic in a Listener that uses @PrePersist to populate the
>> >> value of a required attribute before committing changes. Turns out 
>> >> this
>> >> doesn\u2019t work, since Cayenne invokes validateForInsert() before running
>> >> @PrePersist.
>> >>
>> >> Any suggestions for where I can invoke logic populates required values
>> >> before validation?
>> >>
>> >> Cheers,
>> >> - hugi
>>
>


Re: Validation and @PrePersist

Posted by "Musall, Maik" <ma...@selbstdenker.ag>.
Hi Jurgen,

PostAdd is fine for seting a createDate, but what about an updateDate or something similar? If these assertions are all true:

1. validation is supposed to not change attributes
2. no more changes should be made after validation happened
3. PrePersist runs after validation

then there is just no place left to set something like an updateDate. Also, PrePersist would be restricted to do read-only checks that you can also do during validation, which makes no sense to me conceptually.

Maik



> Am 02.03.2017 um 07:56 schrieb dollj@xsinet.co.za:
> 
> Hi All
> 
> We had a similar discussion Sept-Oct 2012 where it was noted that there was a discrepancy between the docs and the implementation. That was with 3.1B where the behaviour was the same as it is now, i.e. validation occurs before PrePersist (but as noted by Michael the 3.1 docs incorrectly state "after" ).
> 
> So I assume that subsequently the docs were updated for cayenne 4 to correctly reflect the behaviour.
> 
> Back then it was stated that PostAdd was the correct place to do this kind of thing, which I think is fine for fields that have standard default values.
> 
> It can get a bit clumsy sometimes where you sometimes have to have both PostAdd and PrePersist doing the same thing. For example for a timestamp field where object entity creation (PostAdd) occurs much earlier than the commit (PrePersist), but you want the commit timestamp. This is where I would have preferred the behaviour that Hugi is looking for where PrePersist is called first, before validation.
> 
> Cheers,
> Jurgen
> 
> 
> -----Original Message----- From: Michael Gentry
> Sent: Wednesday, March 1, 2017 5:38 PM
> To: Cayenne Users
> Subject: Re: Validation and @PrePersist
> 
> Hi Hugi,
> 
> I was indeed looking at < 4.  No idea why the change was made.  Perhaps
> someone else can answer that one...
> 
> Thanks,
> 
> mrg
> 
> 
> On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
> 
>> Hi Michael,
>> the documentation was apparently changed for 4.0 in 2015 to reflect the
>> current behaviour:
>> 
>> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
>> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>> 
>> Cheers,
>> - hugi
>> 
>> 
>> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
>> >
>> > Hi Hugi,
>> >
>> > If validateForInsert() is being called before PrePersist, we have a
>> > disconnect between the documentation and the behavior you are seeing:
>> >
>> > "PrePersist: right before a new object is committed, inside
>> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
>> > (and prior to validateForInsert())."
>> >
>> > What version of Cayenne are you using?  I wonder if something has > changed
>> > or if the documentation is just wrong.
>> >
>> > Thanks,
>> >
>> > mrg
>> >
>> >
>> >
>> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
>> wrote:
>> >
>> >> Hi all,
>> >> I have some logic in a Listener that uses @PrePersist to populate the
>> >> value of a required attribute before committing changes. Turns out this
>> >> doesn’t work, since Cayenne invokes validateForInsert() before running
>> >> @PrePersist.
>> >>
>> >> Any suggestions for where I can invoke logic populates required values
>> >> before validation?
>> >>
>> >> Cheers,
>> >> - hugi
>> 
> 


Re: Validation and @PrePersist

Posted by do...@xsinet.co.za.
Hi All

We had a similar discussion Sept-Oct 2012 where it was noted that there was 
a discrepancy between the docs and the implementation. That was with 3.1B 
where the behaviour was the same as it is now, i.e. validation occurs before 
PrePersist (but as noted by Michael the 3.1 docs incorrectly state 
"after" ).

So I assume that subsequently the docs were updated for cayenne 4 to 
correctly reflect the behaviour.

Back then it was stated that PostAdd was the correct place to do this kind 
of thing, which I think is fine for fields that have standard default 
values.

It can get a bit clumsy sometimes where you sometimes have to have both 
PostAdd and PrePersist doing the same thing. For example for a timestamp 
field where object entity creation (PostAdd) occurs much earlier than the 
commit (PrePersist), but you want the commit timestamp. This is where I 
would have preferred the behaviour that Hugi is looking for where PrePersist 
is called first, before validation.

Cheers,
Jurgen


-----Original Message----- 
From: Michael Gentry
Sent: Wednesday, March 1, 2017 5:38 PM
To: Cayenne Users
Subject: Re: Validation and @PrePersist

Hi Hugi,

I was indeed looking at < 4.  No idea why the change was made.  Perhaps
someone else can answer that one...

Thanks,

mrg


On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:

> Hi Michael,
> the documentation was apparently changed for 4.0 in 2015 to reflect the
> current behaviour:
>
> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>
> Cheers,
> - hugi
>
>
> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
> >
> > Hi Hugi,
> >
> > If validateForInsert() is being called before PrePersist, we have a
> > disconnect between the documentation and the behavior you are seeing:
> >
> > "PrePersist: right before a new object is committed, inside
> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
> > (and prior to validateForInsert())."
> >
> > What version of Cayenne are you using?  I wonder if something has 
> > changed
> > or if the documentation is just wrong.
> >
> > Thanks,
> >
> > mrg
> >
> >
> >
> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
> wrote:
> >
> >> Hi all,
> >> I have some logic in a Listener that uses @PrePersist to populate the
> >> value of a required attribute before committing changes. Turns out this
> >> doesn\u2019t work, since Cayenne invokes validateForInsert() before running
> >> @PrePersist.
> >>
> >> Any suggestions for where I can invoke logic populates required values
> >> before validation?
> >>
> >> Cheers,
> >> - hugi
>
> 


Re: Validation and @PrePersist

Posted by Michael Gentry <bl...@gmail.com>.
Hi Hugi,

I was indeed looking at < 4.  No idea why the change was made.  Perhaps
someone else can answer that one...

Thanks,

mrg


On Wed, Mar 1, 2017 at 10:03 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:

> Hi Michael,
> the documentation was apparently changed for 4.0 in 2015 to reflect the
> current behaviour:
>
> https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0
> f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f
>
> Cheers,
> - hugi
>
>
> > On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
> >
> > Hi Hugi,
> >
> > If validateForInsert() is being called before PrePersist, we have a
> > disconnect between the documentation and the behavior you are seeing:
> >
> > "PrePersist: right before a new object is committed, inside
> > ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
> > (and prior to validateForInsert())."
> >
> > What version of Cayenne are you using?  I wonder if something has changed
> > or if the documentation is just wrong.
> >
> > Thanks,
> >
> > mrg
> >
> >
> >
> > On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is>
> wrote:
> >
> >> Hi all,
> >> I have some logic in a Listener that uses @PrePersist to populate the
> >> value of a required attribute before committing changes. Turns out this
> >> doesn’t work, since Cayenne invokes validateForInsert() before running
> >> @PrePersist.
> >>
> >> Any suggestions for where I can invoke logic populates required values
> >> before validation?
> >>
> >> Cheers,
> >> - hugi
>
>

Re: Validation and @PrePersist

Posted by Hugi Thordarson <hu...@godurkodi.is>.
Hi Michael,
the documentation was apparently changed for 4.0 in 2015 to reflect the current behaviour:

https://github.com/apache/cayenne/commit/5bb12cd36f0d87e3b217cdc20c22a0f6dc9613f3#diff-5b9a57288e30ef9855d80a63a812ec4f

Cheers,
- hugi


> On 1. mar. 2017, at 14:59, Michael Gentry <bl...@gmail.com> wrote:
> 
> Hi Hugi,
> 
> If validateForInsert() is being called before PrePersist, we have a
> disconnect between the documentation and the behavior you are seeing:
> 
> "PrePersist: right before a new object is committed, inside
> ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
> (and prior to validateForInsert())."
> 
> What version of Cayenne are you using?  I wonder if something has changed
> or if the documentation is just wrong.
> 
> Thanks,
> 
> mrg
> 
> 
> 
> On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:
> 
>> Hi all,
>> I have some logic in a Listener that uses @PrePersist to populate the
>> value of a required attribute before committing changes. Turns out this
>> doesn’t work, since Cayenne invokes validateForInsert() before running
>> @PrePersist.
>> 
>> Any suggestions for where I can invoke logic populates required values
>> before validation?
>> 
>> Cheers,
>> - hugi


Re: Validation and @PrePersist

Posted by Michael Gentry <bl...@gmail.com>.
Hi Hugi,

If validateForInsert() is being called before PrePersist, we have a
disconnect between the documentation and the behavior you are seeing:

"PrePersist: right before a new object is committed, inside
ObjectContext.commitChanges() and ObjectContext.commitChangesToParent()
(and prior to validateForInsert())."

What version of Cayenne are you using?  I wonder if something has changed
or if the documentation is just wrong.

Thanks,

mrg



On Wed, Mar 1, 2017 at 6:18 AM, Hugi Thordarson <hu...@godurkodi.is> wrote:

> Hi all,
> I have some logic in a Listener that uses @PrePersist to populate the
> value of a required attribute before committing changes. Turns out this
> doesn’t work, since Cayenne invokes validateForInsert() before running
> @PrePersist.
>
> Any suggestions for where I can invoke logic populates required values
> before validation?
>
> Cheers,
> - hugi