Proxy-wrapped Sessions (ThreadLocalSessionContext.TransactionProtectionWrapper) handle equals incorrectly

Description

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.


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...


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 aSessionobtained 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:

https://github.com/deduper/hibernate.5.2.2.session.equals.bug/tree/HHH.11067.test.case

Environment

Hibernate Core 5.2.2 Final

Status

Assignee

Steve Ebersole

Reporter

Whoop d'Duper

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Trivial
Configure