in JPA

JPA – Validation Contraints

JPA comes with few default constraints that help validate entity properties.

@Null and @NotNull

Force property to be null or !null.

Accepts any type.

@AssertFalse and @AssertTrue

Force value of the property to false or true.

Accepts Boolean type.

@DecimalMax, @DecimalMin and @Digits

Decimal min and max force the property to be in the specified range. Digits forces the property to be of the specified format.

Accepts BigDecimal, BigInteger, CharSequence, byte, short, int, long with their respective wrappers

@Past and @Future

Force property to be in the correct time frame.

Accepts  java.util.Date and java.util.Calendar types.

note: null is considered valid

@Min, @Max

Force the property value to be in the correct number range.

Accepts BigDecimal, BigInteger, byte, short, int, long, with their respective wrappers

@Pattern

Forces the property value to be of specified pattern.

Accepts CharSequence type.

@Size

Forces the property value to be in the specified length range.

Accepts types of

  • CharSequence (length of character sequence is evaluated)
  • Collection (collection size is evaluated)
  • Map (map size is evaluated)
  • Array (array length is evaluated)

 

Write a Comment

Comment