官术网_书友最值得收藏!

Validations the entity

The Bean Validation API defines many validation constraints that can be applied together on the JPA entities. These are provided in the javax.validation.constraints package.

To apply validations on an entity, you can put the constraints on its fields. Consider our Task entity with validations defined as follows:

@Entity
@Table(name="task_detail")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(min = 1, max = 5)
private String name;

@Pattern(regexp = "[a-zA-Z]")
private String title;

@Future
private LocalDate targetDate;
// Getters & Setters to follow
}

Given the validation constraints defined here, the fields of the Task class will have the following constraints:

  • name: A name cannot contain a null value and must be a minimum length of one and a maximum length of five characters.
  • title: This is constrained by the regular expression pattern, suggesting that only letters of the alphabets are allowed if a value is present. But the value of title may be set to null.
  • targetDate: The valid value can be null or a date in the future, if present.

When trying to persist the bean using an EntityManager, the Bean Validation takes place, and for any value that doesn't meet the defined constraints, there is a violation detected, resulting in a javax.validation.ConstraintViolationException.

These validations are triggered as part of the life cycle of the event, such as pre-persist, pre-update, or pre-remove. These can be controlled by configuring them in the persistence.xml file. Here's a code that tries to save a Task object, but doesn't conform to the constraints listed previously. It would result in an ConstraintViolationException, which we can catch and print to the console:

public void save() {
Task theTask = new Task();
theTask.setName("A longer name than allowed");
try {
em.persist(theTask);
} catch(ConstraintViolationException violation) {
violation.getConstraintViolations().forEach(
v -> System.out.println("violation " + v)
);
}
}

Since the name field has a constraint of @NotNull and @Size defined, this code would result in the following output, as persist would fail:

Info: violation ConstraintViolationImpl{
interpolatedMessage='size must be between 1 and 5',
propertyPath=name,
rootBeanClass=class org.company.project.domain.Task,
messageTemplate='{javax.validation.constraints.Size.message}'
}

It's also possible to override the constraint message by using the message parameter on the annotation during declaration. For example, @NotNull(message = "The task name must not be null").

Now that we have looked at how JPA can trigger these constraint checks as part of the life cycle of events, let's explore validating a entity programmatically. Consider that you have an entity, perhaps as a result of a REST API input, and would like to validate the bean. Here, you can inject a javax.validation.Validator instance and use its validate method to find any ConstraintViolation instances. Here's the snippet to do just that:

@Inject Validator validator;
...
// Some method which takes Task object and validates
Set<ConstraintViolation<Task>> flaws = validator.validate(theTask);
flaws.stream()
.map(ConstraintViolation::getMessage) // Get message string
.forEach(System.out::println); // Print message string

If any violations are found, then the validate method will return a non-zero set of items. That shows you how we can use the programmatic approach when you need more fine-grained control over constraint checks.

主站蜘蛛池模板: 蒙阴县| 成都市| 崇州市| 保康县| 钦州市| 宜州市| 博白县| 津市市| 吉水县| 许昌市| 齐河县| 兴安县| 西充县| 团风县| 炎陵县| 和龙市| 云和县| 肥乡县| 安宁市| 耒阳市| 巴彦淖尔市| 深州市| 旺苍县| 洛浦县| 上饶市| 乌审旗| 绥宁县| 喀什市| 祁门县| 灵川县| 玉龙| 东源县| 静海县| 胶南市| 昂仁县| 昔阳县| 横峰县| 玉林市| 长海县| 吉安市| 铜陵市|