When having an inverse one-to-one bi-directional associations between two entities with orphan removal enabled Hibernate throws a NullPointerExceptions under the following conditions:
The bidirectional references are nulled out.
A manual flush is performed during the transaction.
The NullPointerException is raised in the Cascade.cascadeProperty method on performing the flush on commit of the transaction. The problem is that the code tries to retrieve the EntityEntry for the referenced entity and tries to lookup the entity name for it (see Cascade.java:256ff):
final EntityEntry valueEntry = eventSource.getPersistenceContext().getEntry(loadedValue );
final String entityName = valueEntry.getPersister().getEntityName();
Resolving the entity name fails as the valueEntry is null during the second flush on commit of the transaction. The entity entry has already discarded during the manual flush.
This defect is a relic of fix for issue HHH-5267, which works if you only perform a single flush during the lifecycle of a single entity manager/session.
In versions prior to 4.1.8 (i.e. before fix HHH-5267) the problem was throwing a org.hibernate.PropertyAccessException since it was using the wrong entity name for delete cascading.
I have attached two test cases show casing the error situations caused by the manual flush on these one-to-one associations.
Code wise the test cases are exactly the same but one case uses Hibernate 4.1.8 and the other one 4.1.7.
DB Platforms tested: Oracle, H2