JPA [EcliseLink]:单表继承中无法将父类 A 的实例类型更改为子类 B

问题描述 投票:0回答:1

我在 JavaEE7 应用程序中使用 Eclipselink-2.6 和 wildfly-8 服务器。

我有三个 JPA 实体 A、B 和 C。

B 和 C 延伸 A。

我需要将实体实例 myObjectId A 的类型更改为 B,并保留相同的 id。

我尝试在相同的事务范围中执行以下操作:

1- 将实例“myObjectId”的 dtype 值从“a”更改为“b”。

2- 保存 A,刷新,然后清除缓存,使用 evict,并使用 ((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().initializeAllIdentityMaps()。

3- 通过 id 通过类 b 查找新对象 em.find(B.class, oldId);

这会导致类转换异常,无法将 A 类转换为 B 类!

任何帮助理解这种行为并找到解决方案的人都将不胜感激。

inheritance caching eclipselink jpa-2.0
1个回答
0
投票

这对我有用:

我将即时myObjectId的dtype从A编辑到B,并合并; 分离了实体 myObjectId。

使用以下命令清除 em 的缓存: ((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().initializeAllIdentityMaps() 查找id为myObjectId.id的B类型的实体;

oldId = myObjectId.getId();
myObjectId.setdType("a");
em.merge(myObjectId);
em.detach(myObjectId);

然后,获取新的对象b:

((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().initializeAllIdentityMaps();
B newEntity = em.find(B.class, oldId);
// Edited B as filling its fields...
newEntity = em.merge(newEntity);
© www.soinside.com 2019 - 2024. All rights reserved.