There is a base and xls table with data. It is necessary to drive the data from the table into the database. But before inserting, you need to check the correctness of the data, and indicate errors, if any (mandatory fields, unique keys, string length). How is this better done? Check everything manually in the code or, for example, start a transaction and then cancel it?
2 answers
hibernate has a built-in annotation-based validator.
It looks like this
public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; // ... } Code checking model:
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); Car car = new Car(); Set<ConstraintViolation<Car>> validationErrors = validator.validate(car); if(!validationErrors.isEmpty()) System.out.println("error"); More information can be found at the link Hibernate Validator
|
Transactions nothing to do with, they are responsible for the safety of the data. You basically need a validator that will be responsible for the correctness of a field, but if the validator fails, you cancel the transactions and give an error why this happened. The most difficult thing in this situation is to write a validator, but I think you can be ready to find it.
|