JPA constraints

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Till now we have not validated any entities and its fields, when we wanted some value to be not-null, then not null constraint was applied on database side, i.e. checking of a value being null or not was from database side. Java EE validation aspect provides jpa constraints which enable the developer to validate entities on jpa layer itself.

JPA Constraints:

JPA constraints are the annotations provided to constraint the entities to accept data.These annotations are applied to fields of entities and validation is invoked for entities. Some of JPA constraints are listed below which are directly taken from official oracle tutorial.

Description

Example

@AssertFalse The value of the field or property must be false.
@AssertTrue The
value of the field or property must be true.
@DecimalMax The value of the
field or property must be a decimal value lower than or equal to
the number in the value element.
@DecimalMin The value of the field or property
must be a decimal value greater than or equal to the number in
the value element.
@Digits The value of the field or property must be a
number within a specified range. The integer element specifies the maximum integral digits for
the number, and the fraction element specifies the maximum fractional digits for the
number.
@Future The value of the field or property must be a date in
the future.
@Max The value of the field or property must be an integer
value lower than or equal to the number in the value element.
@Min The value
of the field or property must be an integer value greater than or
equal to the number in the value element.
@NotNull The value of the field
or property must not be null.
@Null The value of the field or property
must be null.
@Past The value of the field or property must be a
date in the past.
@Pattern The value of the field or property must match
the regular expression defined in the regexp element.
@Size The size of the field or
property is evaluated and must match the specified boundaries. If the field or
property is a String, the size of the string is evaluated. If the
field or property is a Collection, the size of the Collection is
evaluated. If the field or property is a Map, the size of the
Map is evaluated. If the field or property is an array, the
size of the array is evaluated. Use one of the optional max or
min elements to specify the boundaries.

In the next article we will learn different type of validatos and how they can be invoked for successful jpa constraints

References for further study :

 

Share Button

1 comment for “JPA constraints

Leave a Reply

Your email address will not be published. Required fields are marked *