I want to do some domain validation
in my object I am having one integer,
now my question is: if I write
@Min(SEQ_MIN_VALUE)
@Max(SEQ_MAX_VALUE)
private Integer sequence;
and
@Size(min = 1, max = NAME_MAX_LENGTH)
private Integer sequence;
If it's integer which one is proper for domain validation.
can anybody explain me what is the difference between them ?
Thanks.
This question is related to
java
spring
validation
annotations
package com.mycompany;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
//getters and setters ...
}
@NotNull
, @Size
and @Min
are so-called constraint annotations, that we use to declare constraints, which shall be applied to the fields of a Car instance:
manufacturer
shall never be null
licensePlate
shall never be null and must be between 2 and 14 characters long
seatCount
shall be at least 2.
From the documentation I get the impression that in your example it would be intended to use:
@Range(min= SEQ_MIN_VALUE, max= SEQ_MAX_VALUE)
Checks whether the annotated value lies between (inclusive) the specified minimum and maximum. Supported data types:
BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types
Source: Stackoverflow.com