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.

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

  @Version
  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 [[com.test.hibernate.entity.locking.A.foo#1066]] for second level caching
    at org.hibernate.engine.loading.internal.CollectionLoadContext.addCollectionToCache(CollectionLoadContext.java:360) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:299) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
    at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[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 application.properties:

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.JCacheRegionFactory
spring.jpa.properties.hibernate.cache.default_cache_concurrency_strategy=read-write
spring.jpa.properties.hibernate.cache.provider_class=org.ehcache.jsr107.EhcacheCachingProvider
spring.jpa.properties.hibernate.cache.missing_cache_strategy=create

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(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue.