Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Automatic entity validation for Container Constaints, doesn't work(Validationg a List of Items )
Automatic entity validation for Container Constaints, doesn't work [message #1837042] Thu, 21 January 2021 10:37
Francesco KronosOne is currently offline Francesco KronosOneFriend
Messages: 1
Registered: January 2021
Junior Member
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!
Previous Topic:EL 2.7 JPA-api classess inside eclipselink jar
Next Topic:PerformanceMonitor Log output when used in Payara
Goto Forum:
  


Current Time: Sun Jun 13 00:31:22 GMT 2021

Powered by FUDForum. Page generated in 0.02014 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top