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

Reply via email to