How To Validate A Javaee6 Edible Bean Inwards A Job

Normally edible bean validation is already integrated inwards frameworks similar PrimeFaces, simply what if we're working on information inwards a batch job? In these cases nosotros won't convey access to these libraries. So how create nosotros validate our bean? Here's how I did it.

For representative nosotros convey an entity edible bean Customer:
@Entity @Table(name = "CUSTOMER") world course of pedagogy Customer implements Serializable {   @Id  @GeneratedValue(strategy = GenerationType.SEQUENCE)  someone Long id;     @NotNull  @Size(min = 1, max = 50)  @Column(name = "FIRST_NAME")  someone String firstName;    @NotNull  @Size(min = 1, max = 50)  @Column(name = "LAST_NAME")  someone String lastName;   } 

To validate nosotros remove to inject the edible bean Validate together with validate our bean, here's how:
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.Validator;  @Stateless world course of pedagogy CustomerImport {   @Inject  someone Validator validator;    someone void validate(Customer customer) {   Set<ConstraintViolation<Customer>> violations = validator.validate(customer);      if (!violations.isEmpty()) {    throw novel ConstraintViolationException(      novel HashSet<ConstraintViolation<?>>(violations));   }  }    } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.