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

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.

主站蜘蛛池模板: 江山市| 光山县| 桦川县| 大同县| 静乐县| 古丈县| 衡东县| 城口县| 合江县| 铜陵市| 神池县| 铜陵市| 会东县| 建湖县| 宝鸡市| 杭州市| 吉木乃县| 万年县| 皋兰县| 牡丹江市| 蒲城县| 湖口县| 维西| 亳州市| 柏乡县| 广德县| 三门县| 左贡县| 清河县| 扎兰屯市| 淄博市| 云和县| 安达市| 兴化市| 固始县| 钦州市| 永德县| 毕节市| 奉新县| 遵义市| 南安市|