Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix validation constraints in embedded object beeing ignored?

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

like image 671
Shaiby Avatar asked Jan 09 '19 08:01

Shaiby


People also ask

How do you validate a constraint?

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 .

What is the use of @valid annotation?

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.

What does javax validation constraints NotNull do?

@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.

What is javax validation constraints?

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.


1 Answers

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;

    ...
}
like image 93
veljkost Avatar answered Sep 19 '22 08:09

veljkost