O'Reilly logo

Java EE 7 Developer Handbook by Peter A. Pilgrim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying constraint definitions

As we have seen, applying built-in constraints to value objects is almost painless. What happens if you want a constraint that is not covered by the built-in types? Bean Validation allows the developer to write custom constraints.

Let's review a value object that has an entity relationship with another one. Here is the code for the Country object:

package je7hb.beanvalidation.essentials;
import org.hibernate.validator.constraints.NotEmpty;

public class Country {
    private String isoName;
    @NotEmpty
    public String getISOName() { return isoName; }
    public Country() { }
}

This is the Address object, the master of the detail:

package je7hb.beanvalidation.essentials; import javax.validation.Valid; import javax.validation.constraints.*; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required