When obtaining a "current Session" backed by {{ ThreadLocalSessionContext}}, the returned Session is actually a proxy. When {{Session#equals}} is called on this proxy, Hibernate delegates the call to the wrapped Session which then incorrectly compares itself against a proxy form of itself. Instead the proxy should handle the {{Session#equals}} call relative to the proxy instance itself.
----
h2. Original (largely inaccurate and argumentative) description
As of the 5.2.2 release, the {{SessionImpl.equals(Object)}} method ignores the following advice recommended in _[the Java API documentation|https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)]_...
---- {quote} The {{equals}} method implements an equivalence relation on non-null object references:
It is _reflexive_: for any non-null reference value {{x, x.equals\(x\)}} should return {{true}}. It is _symmetric_: for any non-null reference values {{x}} and {{y, x.equals\(y\)}} should return {{true}} if and only if {{y.equals\(x\)}} returns {{true}}. It is _transitive_: for any non-null reference values {{x, y,}} and {{z}}, if {{x.equals\(y\)}} returns {{true}} and {{y.equals(z)}} returns {{true}}, then {{x.equals\(z\)}} should return {{true}}. It is {{consistent}}: for any non-null reference values {{x}} and {{y}}, multiple invocations of {{x.equals\(y\)}} consistently return {{true}} or consistently return {{false}}, provided no information used in {{equals}} comparisons on the objects is modified. For any non-null reference value {{x}}, {{x.equals\(null\)}} should return {{false}}. {quote} ---- Given a non-null instance, {{Session aSession}} — _obtained through a call on_ {{SessionFactory.getCurrentSession()}} — the expected behavior of {{aSession.equals(aSession)}} should be a boolean return value of {{true}}.
The actual behavior — _as of the 5.2.2. release_ — is to confusingly return {{false}}.
How "_idiomatic_" is it for a Java object to not be equal to itself?
I confirmed this bug exists in 5.2.2 after asking this question in _[a Question-Answer forum|http://stackoverflow.com/questions/39155751/is-this-a-bug-in-org-hibernate-internal-sessionimpl]_
----
*_The Test Case to reproduce the bug, can be found here_*:
https://github.com/deduper/hibernate.5.2.2.session.equals.bug/tree/HHH.11067.test.case |
|