Encountering not-null constraint violations related to a secondary table for an entity in Hibernate
Environment
- Red Hat JBoss Enterprise Application Platform (EAP) 7.2
- Hibernate 5.3
Issue
-
An entity is defined with a non-optional secondary table
-
After migrating from an older EAP / Hibernate release to EAP 7.2 (Hibernate 5.3), encountering
not-nullconstraint violations in various scenarios for properties persisted in the secondary table... java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("MYSCHEMA"."MY_JOIN_TABLE"."APROPERTY") at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:11190) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:244) at org.jboss.jca.adapters.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:1180) at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:118) ...... javax.validation.ConstraintViolationException: Validation failed for classes [org.mypackage.MyEntity] during update time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=aproperty, rootBeanClass=class org.mypackage.MyEntity, messageTemplate='{javax.validation.constraints.NotNull.message}'} ] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:140) at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:88) at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:248) ... at org.hibernate.query.Query.getResultList(Query.java:135) ...
Resolution
- Address the corrupt data in the database (supply the missing row).
- The change in loading behavior that made Hibernate more vulnerable to corrupt data in this scenario will be addressed in a future release1.
Root Cause
- The secondary table was corrupted and a required row was missing.
- A change in loading behavior in Hibernate 5.3 introduced a vulnerability to corrupt data in the underlying database
- This is a known defect (Content from hibernate.atlassian.net is not included.HHH-13424).
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.