java - @Valid and custom validation annotation - when does validation take place? -


i develop application , found cant understand. have created custom validation annotation called @usernameavailable , implementation of constraintvalidator.

i annotate field of entity annotation , have added system.out.println("something message"); inside constraintvalidator isvalid() method.

what have noticed validation method called twice:

at first while form on page submited , data binded entity. and then, second time when entity saved jparepository (from spring data jpa)

but difference that, in constraintvalidator implementation have @autowired dependencies , when validation takes place first time on "form submit validation" dependencies injected properly.

and @ second time, while isvalid method called on saving entity @autowired service null, why that?

if dont understand read , watch that:

all of wrote shown on video: https://www.youtube.com/watch?v=rff0caxahvc (best, start watching 04:20 minute)

this exact problem have, why have check nulls in isvalid? why service null?

its quite imporant me understand that

i have encountered same issue in java ee (jsf/cdi/ejb/jpa) app. think happening here same.

you have 2 different containers validation performed here. form submitted (in case cdi managed bean), , within ejb when jpa creates , calls validator prior persisting.

for form validation container creates validator supports injection. however, subsequent form validation entity submitted ejb service (may differ in case) in turn calls jpa/entitymanager persist etc. jpa has validation step prior persisting, not create such validator (with cdi capabilities) injection attempt fail have seen. there no requirement in jpa spec support injection in validation step , have tested eclipselink , hibernate.

there several options available around – none seem elegant - atleast in scenarios such yours - having jpa support custom validators cdi capabilities. purpose of answer can confirm behaviour observe specified/designed reasons given. have had same problem here ejb injection fails in custom constraintvalidator on jpa persist.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -