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)); } } }
0 komentar:
Please comment if there are any that need to be asked.