如何解决Hibernate LazyLoading + Cache + Optimistic Locking 抛出 HibernateException
我遇到了 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 举报,一经查实,本站将立刻删除。