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.
As of the 5.2.2 release, the SessionImpl.equals(Object) method ignores the following advice recommended in the Java API documentation...
The equals method implements an equivalence relation on non-null object references:
It is reflexive: for any non-null reference value x, x.equals should return true.
It is symmetric: for any non-null reference values x and y, x.equals should return true if and only if y.equals returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals 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 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.
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
The Test Case to reproduce the bug, can be found here:
Hibernate Core 5.2.2 Final