I have my entity object in Spring Boot & Hibernate REST API. This class has many fields. Part of them is embedded and validation constraints such as @Min @Max are not working on fields in @Embeddable class. Same validation rules work perfect in @Entity classes. I am using javax.validation.constraints.Max
My main object looks like this:
@Entity
public class Notice extends BaseEntity {
@Embedded
private MyEmbeddedClass myEmbeddedClass;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "entity_class_id")
private MyEntityClass myEntityClass;
}
And my @embedded class:
@Embeddable
public class MyEmbeddedClass {
@Size(max = 50)
private String label;
@Max(100)
private Integer percent;
}
@Max constraint on percent field is ignored, but @size is working perfectly
@Entity
public class MyEntityClass extends BaseEntity {
@Size(max = 50)
private String name;
@Max(6000)
private Integer size;
}
And here @Max constraint and @size constraint on fields size are beeeing created
Is there a way to fix this? My Spring boot version is 2.1.1 and I can create my database scripts manually but I'd like to avoid that and get almost perfect script thanks to hibernate
The VALIDATE CONSTRAINT statement is part of ALTER TABLE and checks whether values in a column match a constraint on the column. This statement is especially useful after applying a constraint to an existing column via ADD CONSTRAINT .
The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.
@NotNull validates that the annotated property value is not null. @AssertTrue validates that the annotated property value is true. @Size validates that the annotated property value has a size between the attributes min and max; can be applied to String, Collection, Map, and array properties.
Package javax. validation. constraints Description. Contains all the Bean Validation provided constraints also called built-in constraints. These constraints do not cover all functional use cases but do represent all the fundamental blocks to express low level constraints on basic JDK types.
You need to add @Valid
annotation on your embedded object if you want to validate the constraints defined in your @Embeddable
object:
@Entity
public class Notice extends BaseEntity {
@Embedded
@Valid
private MyEmbeddedClass myEmbeddedClass;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With