- 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.
- UI設計基礎培訓教程
- Instant Testing with CasperJS
- SpringMVC+MyBatis快速開發(fā)與項目實戰(zhàn)
- Learning ArcGIS Pro
- PhoneGap Mobile Application Development Cookbook
- Modern JavaScript Applications
- C語言程序設計學習指導與習題解答
- 深入RabbitMQ
- Mastering JavaScript Design Patterns(Second Edition)
- Microsoft Dynamics AX 2012 R3 Financial Management
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Unity 3D/2D移動開發(fā)實戰(zhàn)教程
- Emgu CV Essentials
- 微信小程序開發(fā)實戰(zhàn):設計·運營·變現(xiàn)(圖解案例版)
- 從Excel到Python數(shù)據(jù)分析:Pandas、xlwings、openpyxl、Matplotlib的交互與應用