I have to add before UPDATE validation for the entity field.
There is a 'status' enum field, that has 3 values(A,B,C).
So if entity has value C, it cannot be updated on A or B.
SQL triggers are not allowed.
I found @SQLUpdate annotation and @Where annotation, but cannot find how to use this annotations.
I tried to use @EntityListeners, but listener has entity object in argument that was already updated, but there is no access to the current value from database.
public class MyEntityListener {
@PreUpdate
private void validateStatusFieldForUpdate(MyEntity entity) {
if (!(entity.getStatus().equals(entity.getStatus()))
&& entity.getStatus().equals(MyEntityStatusEnum.C)) {
throw new ListenerValidationException("Entity with status='C' cannot be updated!");
}
}
}
Is a good practice to do these validations out of the persistence layer (entities & repositories)
Your case is a classic data validation that you can resolve using a validate method in static class, by example, called from any service class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.