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

  • Java EE 8 and Angular
  • Prashant Padmanabhan
  • 509字
  • 2021-07-02 19:22:36

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.

主站蜘蛛池模板: 海门市| 成武县| 峨眉山市| 十堰市| 祁东县| 滕州市| 齐河县| 秭归县| 哈尔滨市| 正镶白旗| 松潘县| 景德镇市| 杨浦区| 班戈县| 石屏县| 绵阳市| 南漳县| 三亚市| 承德市| 礼泉县| 根河市| 托里县| 从化市| 余江县| 庆云县| 砀山县| 柞水县| 神池县| 马鞍山市| 鹤山市| 鄯善县| 浮梁县| 青州市| 共和县| 本溪| 台东县| 青铜峡市| 米泉市| 大埔区| 汶川县| 原阳县|