Code improvement suggestion in CollectionEntry.postInitialize for 6.5.0Final to cast to interface type and not abstract class

Hi,
we have a quite custom legacy solution migrating to Hiberate 6.5.0Final currently. So, maybe noone else will face this ClassCastException in CollectionEntry.postInitialize caused by casting to AbstractPersistentCollection in line

final SharedSessionContractImplementor session = ((AbstractPersistentCollection<?>)collection).getSession();

Why do we face this issue? Our custom collection class implements PersistentCollection but can not extend AbstractPersistentCollection.

Basically a possible solution to overcome this ClassCastException and having a cleaner casting to the interface type instead of casting to an abstract class can be achieved by extending PersistentCollection by a default method:

  default SharedSessionContractImplementor getSession() {
    return null;
  }

So the cast to the abstract class can be replaced by casting to the interface type PersistentCollection:

final SharedSessionContractImplementor session = ((PersistentCollection<?>)collection).getSession();

Happy to hear from you if this might be considered as an improvement.

This definitely looks like a bug. Would you mind creating a Jira issue?

I suspect @gavinking , who introduced the cast, will want to have a look.

EDIT: To clarify, your solution may be fine, and if so it’d be great if you could also send a PR, but I suspect we might want to plan for forcing PersistentCollection implementors to implement getSession in the future, which requires additional planning.

Agree, not having any casting at all would be the best solution. To send a PR is unfortunately not possible.

Creating a JIRA issue is in general no problem. Do you have a link to your JIRA?

https://hibernate.atlassian.net/browse/HHH

See also Come and join us! - Hibernate

[HHH-18067] - Hibernate JIRA created.