- JPAのトランザクション中でこんなエラーが出る。 (JPAが内部的に行っている Validation であるため、ユーザプログラムが最後に行った操作が原因ではない! たちが悪い)
javax.validation.ConstraintViolationException:
Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'.
Please refer to embedded ConstraintViolations for details.
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.validateOnCallbackEvent
(BeanValidationListener.java:90)
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.preUpdate(BeanValidationListener.java:72)
at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:671)
at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyEJB30Listeners(DescriptorEventManager.java:641)
at org.eclipse.persistence.descriptors.DescriptorEventManager.executeEvent(DescriptorEventManager.java:200)
...
- 何がどういけないのかわからん? "Please refer to embedded ConstraintViolations? for details." って何すりゃいいのさ!
- → わかりにくいけど、言われたとおり ConstraintViolationException? を解析すればいい
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException cve = (ConstraintViolationException) ex;
for (ConstraintViolation cv : cve.getConstraintViolations()) {
System.out.println("CONSTRAINT VIOLOATION : " + cv.toString());
}
}
- 実行結果
CONSTRAINT VIOLOATION : ConstraintViolationImpl{interpolatedMessage='size must be between 1 and 2',
propertyPath=iataCode, rootBeanClass=class com.mycompany.travelbook.entity.AirlineTbl,
messageTemplate='{javax.validation.constraints.Size.message}'}
- ふぅ