| Unproxying a lazyly loaded object in a getter method was never a problem up to version 5.1, but in 5.2 Hibernate somehow tries to delete an associated object (OneToOne) from the database although the association was never changed. Please have a look at the test case I created, I think this should make it clear: https://github.com/mpe85/hibernate-unproxy-problem (Test class JPAUnitTestCase) Relevant code snippet:
@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.LAZY)
public Child getChild() {
return (Child) Hibernate.unproxy(child);
}
Note: Hibernate.unproxy() was introduced in 5.2, the 5.1 (and prior) code of course looked like this:
((HibernateProxy)entity).getHibernateLazyInitializer().getImplementation();
I guess Hibernate expects the getter to return the proxy, but finds the unproxied object and concludes the association was altered (causing the deletion attempt of the child). Hibernate should treat the proxy object and the unproxied object as the same object like in 5.1 or earlier so that the unproxying can still be done in the getter (which is very nice because it is a very central place and the caller does not have to care about unproxying). Please clarify if this is a bug or if this behaviour was introduced intentionally. |