I need to validate a JPA entity Transaction) with custom constraint when receiving a List . Then I create a Custom Constraint Annotation:
@Target({ ElementType.TYPE,ElementType.TYPE_USE })
@retention(RUNTIME)
@documented
@constraint(validatedBy = { TransactionValidator.class})
public @interface TransactionValid {
String message() default "{org.hibernate.validator.referenceguide.chapter06.classlevel." +
"ValidPassengerCount.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
LoadValues type() default LoadValues.MOV;
@interface List {
TransactionValid[] value();
}
}
And referenced Validator :
public class TransactionValidator implements ConstraintValidator<TransactionValid,Transaction>{
@override
public void initialize(TransactionValid constraintAnnotation) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@override
public boolean isValid(Transaction t, ConstraintValidatorContext context) {
try {
ValidatorObject validator=new TransactionValidatorLogic();
validator.validate(t);
}
catch (ConstraintViolations ex) {
for (String s :ex.getViolations())
context.buildConstraintViolationWithTemplate(s).addConstraintViolation();
return false;
}
return true;
}
}
Then , in my EJB I have the method receiving the List of Transaction :
public void loadTransactions(List<@TransactionValid Transaction> q,final DataInfo info) throws HBException {
// some other stufs
try{
for (Transaction t : q){
em.persist(t); } }
catch (Exception e){
if (e instanceof ConstraintViolationException){
}
....
}
Now I am expecting to get a breakpoint on my Custom Validator but it seams no Validation take place .
Thi is an EAR project with EJB and WEB modules . IN the EJB pom I used :
<dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>9.0.0</version> <scope>provided</scope> </dependency>
And my persistence.xml is attatched
As you cans see I added the property validation-mode though it is not needed states the Jakarta Persistence Specification 3.0 .The specifiaction states that by default the AUTO value is setted and by the way the validation Bootstrap will be done by Container looking in the List and applying the Validation to all item in the List ,then what I am wrong ? it is a bug ?
If I apply the @TransancionValid annotation to Transaction Entity this working correct and fire up my Validator.
Elsewhere ,I doen't see any validation in debugging ,any breakpoint stop on Validator!