I have entity :
@NotNull
@Column(name = "version")
private String version;
And I also need that the version should be only : 2,5,7,11 or 18.
If another, it will not valid for passing.
Example :
If the version equals 5, it is okay.
If the version equals 6, it is NOT okay.
How can I implement it?
CodePudding user response:
You can validate using @Pattern annotation. See this link. For your case:
import javax.validation.constraints.Pattern;
@NotNull
@Column(name = "version")
@Pattern(regexp="^(2|5|7|11)$",message="Invalid Version")
private String version;
This should work.
CodePudding user response:
You can write validation logic in setter methods.
public void setVersion(String version){
if(version.equlas("2") ||
version.equlas("5") ||
version.equlas("7") ||
version.equlas("11")||
version.equlas("18")){
this.version= version;
}else{
// do something that you want if value is NOT equal to 2,5,7,11 or 18.
}
}
You can validate the value of the version in the service
layer too. Or you can use a method to validate it or even a separate validator class. There are a lot of possibilities. It's all depends on your choice.