getIdentifierMethod of BasicLazyInitializer does not match if method invoked through an interface with different return type

Description

I have an entity interface,

public interface IEntity {
public Object getId();
}

and I have an entity, say User that implements this interface with an id method

@Id
public Long getId() {
return id;
}

I can call getId() on an uninitialized proxy with,

user.getId()

however, if I call getId() in this way,

((IEntity) user)).getId()

I get org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Looking at the code, BasicLazyInitializer has these lines,

else if ( isUninitialized() && method.equals(getIdentifierMethod) ) {
return getIdentifier();
}

the method.equals(getIdentifierMethod) returns false. The methods are not equal() because they have different return types (this is allowed in 1.5 with covariant return types).

Hibernate should not use Method.equals(), but should instead use the code below (modified from Method.equals(Object)),

public boolean equalsIgnoringReturnType(Method m1, Method m2) {

if ((m1.getDeclaringClass() != m2.getDeclaringClass()) {
return false;
}
if (m1.getName() != m2.getName()) {
return false;
}

Class[] params1 = m1.getParameterTypes();
Class[] params2 = m2.getParameterTypes();
if (params1.length != params2.length) {
return false;
}
for (int i = 0; i < params1.length; i++) {
if (params1[i] != params2[i])
return false;
}
return true;
}

Environment

None

Status

Assignee

Unassigned

Reporter

Sean Bridges

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

3.3.1

Priority

Minor