- 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.
- Functional Kotlin
- SQL Server數據庫管理與開發兵書
- Access 2010數據庫應用技術實驗指導與習題選解(第2版)
- Unity Android Game Development by Example Beginner's Guide
- C語言程序設計實踐
- Docker:容器與容器云(第2版)
- HTML5 WebSocket權威指南
- 軟技能2:軟件開發者職業生涯指南
- Java Script從入門到精通(第5版)
- 循序漸進Vue.js 3前端開發實戰
- 面向對象程序設計教程(C#版)
- 移動智能系統測試原理與實踐
- LabVIEW案例實戰
- Mastering Rust
- 梔子貓的奇幻編程之旅:21天探索信息學奧賽C++編程