The bean validation API also supports graph validation, which means that if some object is referencing another one, you can define a constraint that your embedded object should also be validated from the owner's one context, according to the constraints defined in the embedded object.
Let's try this with an example; we will define a Producer
class with a constraint on the name attribute as follows:
public class Producer { @NotNull private String name; // setters and getters here }
Then, we will modify our Movie
object so it references an instance of the Producer
object, as follows:
public class Movie { @NotNull private String title; @Valid private Producer producer; public Movie() { } // setters and getters here }
Note the @Valid
attribute; it tells the bean validator that when validating the Movie
object, the Producer
object should also be validated.
Now, let's try to create a movie and producer objects with null...