Search

1.2. Built in constraints

download PDF
Hibernate Validator comes with some built-in constraints, which covers most basic data checks. As we'll see later, you're not limited to them, you can literally in a minute write your own constraints.
Table 1.1. Built-in constraints
Annotation Apply on Runtime checking Hibernate Metadata impact
@Length(min=, max=) property (String) check if the string length match the range Column length will be set to max
@Max(value=) property (numeric or string representation of a numeric) check if the value is less than or equals to max Add a check constraint on the column
@Min(value=) property (numeric or string representation of a numeric) check if the value is more than or equals to min Add a check constraint on the column
@NotNull property check if the value is not null Column(s) are not null
@NotEmpty property check if the string is not null nor empty. Check if the connection is not null nor empty Column(s) are not null (for String)
@Past property (date or calendar) check if the date is in the past Add a check constraint on the column
@Future property (date or calendar) check if the date is in the future none
@Pattern(regex="regexp", flag=) or @Patterns( {@Pattern(...)} ) property (string) check if the property match the regular expression given a match flag (see java.util.regex.Pattern ) none
@Range(min=, max=) property (numeric or string representation of a numeric) check if the value is between min and max (included) Add a check constraint on the column
@Size(min=, max=) property (array, collection, map) check if the element size is between min and max (included) none
@AssertFalse property check that the method evaluates to false (useful for constraints expressed in code rather than annotations) none
@AssertTrue property check that the method evaluates to true (useful for constraints expressed in code rather than annotations) none
@Valid property (object) perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively. none
@Email property (String) check whether the string is conform to the email address specification none
@CreditCardNumber property (String) check whether the string is a well formated credit card number (derivative of the Luhn algorithm) none
@Digits(integerDigits=1) property (numeric or string representation of a numeric) check whether the property is a number having up to integerDigits integer digits and fractionalDigits fractonal digits define column precision and scale
@EAN property (string) check whether the string is a properly formated EAN or UPC-A code none
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.