I need to implement range constraints on Entity data fields:
@Entity
@Table(name = "T_PAYMENT")
public class Payment extends AbstractEntity {
//....
//Something like that
@Range(minValue = 80, maxValue = 85)
private Long paymentType;
}
I already created validating service, but have to implement many of these cases.
I need the app to throw exception if the inserted number is out of range.
You need Hibernate Validator (see documentation)
Hibernate Validator
The Bean Validation reference implementation.
Application layer agnostic validation Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming. But a simple example says more than 1000 words:
public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; // ... }