LazyLoading + Cache + Optimistic Locking throws a HibernateException

Hey folks,
i am facing a problem with Hibernate lazy loading in combination with the 2nd level cache and optimistic locking. I have an entity that is cached in the 2nd level cache. This entity has a lazy loaded collection which should also be cached.

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class A
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long identifier;

  private long entityVersion;

  @ElementCollection(fetch = FetchType.LAZY)
  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  private List<String> foo = new ArrayList<>();

When I try to work (load or add Objects) with the collection I am getting a HibernateException:

Caused by: org.hibernate.HibernateException: Unable to resolve owner of loading collection [[]] for second level caching
    at org.hibernate.engine.loading.internal.CollectionLoadContext.addCollectionToCache( ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection( ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections( ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections( ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading( ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]

If I try to debug this code the exception disappears (maybe because the debugger is loading the collection beforehand?). The same behaviour is shown if I catch the HibernateException and try to work with the collection again. The FetchMode “join” does help, but the collection won’t be loaded lazy then.

These are the properties in my

This behaviour is also shown in a non spring boot application.

I am using Hibernate 5.4.29.Final and ehcache 3.9.3. Any idea what the problem is? Please let me know if you need more information.

Hi, I think this might be a bug. Please create an issue in the issue tracker( with a test case( that reproduces the issue.