问题描述
我遇到了 Hibernate 延迟加载与二级缓存和乐观锁定相结合的问题。 我有一个缓存在二级缓存中的实体。这个实体有一个延迟加载的集合,它也应该被缓存。
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Cacheable
public class A
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long identifier;
@Setter(AccessLevel.NONE)
private String uuid = UUID.randomUUID().toString();
@Version
private long entityVersion;
@ElementCollection(fetch = FetchType.LAZY)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private List<String> foo = new ArrayList<>();
}
当我尝试使用集合工作(加载或添加对象)时,我收到了 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]
如果我尝试调试此代码,异常就会消失(可能是因为调试器预先加载了集合?)。如果我捕获 HibernateException 并尝试再次使用该集合,则会显示相同的行为。 FetchMode“join”确实有帮助,但该集合不会被延迟加载。
这些是我的 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
我使用的是 Hibernate 5.4.29.Final 和 ehcache 3.9.3。 知道问题是什么吗?如果您需要更多信息,请告诉我。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)