Have been assigned to understand why we cannot step version beyond 5.2.2. The project has been running for a couple of years using Hibernate but when trying to step up from 5.2.2 it fails with some exception. I've located the problem to the introduction of https://hibernate.atlassian.net/browse/HHH-11097. The problem seems to origin in:

We get an exception in the toLoggableString() due to that the object that is a Set and we get e cast to CollectionType but it is not a Collection and when it tries to get the iterator from it it crashes. However in the 5.2.2 version the code above is pretty much the same (excepti for the toLoggableString()) and I can see that we have exactly the same behvour, i.e. the object is not a Collection but here it seems not to matter since no iteration is done. Understand if the problem description is a bit thin but perpaps someone has seen something similar.

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Collection at org.hibernate.type.CollectionType.getElementsIterator(CollectionType.java:250) at org.hibernate.type.CollectionType.renderLoggableString(CollectionType.java:208) at org.hibernate.type.CollectionType.toLoggableString(CollectionType.java:198) at org.hibernate.type.TypeHelper.toLoggableString(TypeHelper.java:439) at org.hibernate.cache.spi.entry.StructuredCacheEntry.destructure(StructuredCacheEntry.java:54) at org.hibernate.event.internal.DefaultLoadEventListener.processCachedEntry(DefaultLoadEventListener.java:612) at org.hibernate.event.internal.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:602) at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:462)

1. How can you have a CollectionType if the underlying attribute is not a collection? Add your mapping to get a better understanding of your use case.

2. Do you happen to include a Collection in toString? That's quite wrong since you don't want toString to trigger a Collection initialization, or to fail if you didn't initialize the collection and you closed the origin Persistence Context. Again, we need to see your mappings.

Some more findings before I'll try to make a "simple" testcase for this... s

The target system is rather big with more than 1000 database requests/seq during peak and it has worked well for a few years but with the HHH-11097 it does not anymore. Have verified that if we do not use the L2Cache it works. Have also verified that if we have the L2Cache enabled and "removes" the creation of the logstring as below it works as before (except for log output) . My conclusion is that we must have run into some form of cornercase here.

Have been able to reproduce the fault in a simple Maven context. The problem drills down to the embedded identifier Key in the HibernateRoot.class in combination that the object is retreived from the second level cache. The root cause prior the exception below is in CollectionType:toLoggableString() on the line if ( persister.getKeyType().getReturnedClass().isInstance( value ) ) which should return true but is false. The returned class is Key but value is Object[].Integer. If we replace the embeded id wiht a simple integer it works well.

Callchain:Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Collection at org.hibernate.type.CollectionType.getElementsIterator(CollectionType.java:249) at org.hibernate.type.CollectionType.renderLoggableString(CollectionType.java:207) at org.hibernate.type.CollectionType.toLoggableString(CollectionType.java:197) at org.hibernate.type.TypeHelper.toLoggableString(TypeHelper.java:439) at org.hibernate.cache.spi.entry.StructuredCacheEntry.destructure(StructuredCacheEntry.java:54) at org.hibernate.event.internal.DefaultLoadEventListener.processCachedEntry(DefaultLoadEventListener.java:614) at org.hibernate.event.internal.DefaultLoadEventListener.loadFromSecondLevelCache(DefaultLoadEventListener.java:604) at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:463)