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 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 <h...@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 <blackn...@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 <h...@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



Reply via email to