I want to set values from an dto object and insert them in database with save jpa repository methode can i insert a default value if attributes is not valid(null or size is to longer) using jpa ?
dto object :
public class SocleList implements Serializable {
private static final long serialVersionUID = -5008625218377596565L;
@JsonProperty("records")
@Valid
private List<Socle> SocleList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Socle implements Serializable {
private static final long serialVersionUID = -1864310486952111265L;
@NotNull
public int iDTechn;
@NotNull
@Max(value = 99999)
private Integer code;
@NotNull
private String codeType;
@NotNull
@Size(min = 1, max = 2)
private String red;
private @NotNull
@Max(value = 99999)
Integer FPrinci;
private @NotNull
@Max(value = 99999)
Integer surface;
@NotNull
@Size(min = 1, max = 2)
private String Xnseigne;
}
I use a validator to control values :
public static int validate(SocleList SocleList,
Validator validator) {
Set<ConstraintViolation<SocleList>> violations;
violations = validator.validate(socleList);
log.info("Nombre de violations : {}", violations.size());
for (ConstraintViolation<SocleList> constraintViolation : violations) {
log.info("Valeur '{}' incorrecte pour '{}' : '{}' "
, constraintViolation.getInvalidValue()
, constraintViolation.getPropertyPath()
, constraintViolation.getMessage());
}
return violations.size();
}