所以,我有 A 类和 B 类。
他们使用以下配置共享主键:
在 A 类中,我小时候引用了 B 类
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public B getB()
{
return b;
}
在 B 类中,为了从父类 A 获取 ID,我使用以下注释:
@Id
@GeneratedValue(generator = "customForeignGenerator")
@org.hibernate.annotations.GenericGenerator(name = "customForeignGenerator", strategy = "foreign", parameters = @org.hibernate.annotations.Parameter(name = "property", value = "a"))
@Column(name = "a_id")
public Long getId()
{
return id;
}
@MapsId("id")
@OneToOne(mappedBy = "b")
@PrimaryKeyJoinColumn
public A getA()
{
return a;
}
问题是用
保存Asession.saveOrUpdate(aInstance);
数据库返回以下错误:
Duplicate entry '123456' for key 'PRIMARY'
这告诉我们两件事,首先是 @MapsId
工作正常,将 A 的 Id 给了 B,第二个是 hibernate 决定它是一个“保存”而不是一个“update',这只发生在 saveOrUpdate
上,当 Id 为 null 时,对吗? (奇怪?)
通常的解决方案是从 DB 中获取
旧的 B 并合并
,如果存在的话,但这会带来很多问题,比如从中获取旧的 A数据库到 session 或使可怕的“具有相同标识符值的不同对象已与 session 关联
”关联对象的 hibernate 错误。性能也不是很友好,执行不必要的数据库命中。
我的注释有错误吗?我做错了吗?这个的正常配置是什么?
编辑:
这有点违背了使用 @MapsId
的目的,手动设置 ID,但由于没有找到解决方案,我确实像这样手动设置了 ID:
if(aInstance.getId() != null)
aInstance.getB().setId(aInstance.getId());
session.saveOrUpdate(aInstance);
就在不久之前,它返回了以下错误:
org.hibernate.StaleStateException:
Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
但由于某种原因,它停止抛出错误,现在它可以工作了。在所有情况下,之前的代码仍然有效,因为 aInstance
可能没有 Id,在这种情况下,MapId 可以完美地在 BD 中插入新的 A 和 B。问题仅出现在更新上。
是\这是一个 hibernate 错误吗?大概。当 StaleStateException
再次出现时,我会通知你们。
目前这是一个临时解决方案,直到有人提出实际解决方案。
最佳答案
我终于找到了所有问题的答案。
要了解问题的根源,我们必须提醒一下 saveOrUpdate(object)
是如何工作的。
1)如果对象
设置了ID,saveOrUpdate
将更新
否则它将保存
.
2) 如果 hibernate 决定它是一个Save
但对象已经在 DB 上,您想要更新,Duplicate entry '123456' for key ' PRIMARY'
会发生异常。
3) 如果hibernate 决定它是一个Update
但对象不在DB 中,您想要保存,StaleStateException
异常发生。
问题 依赖于这样一个事实,即如果 aInstance
存在于数据库中并且已经有一个 ID,@MapsId
会将那个 ID 提供给 B
忽略上面的规则,让 Hibernate 认为 B
也存在于 DB 中,而实际上它可能不存在。只有当 A
和 B
不存在于数据库中或它们都存在时,它才能正常工作。
因此变通解决方案是确保您设置
ID only and only 如果每个对象都存在于数据库中,并将ID设置为不存在时为 null:
B dbB = (B) unmarshaller.getDetachedSession().createCriteria(B.class).add(Restrictions.idEq(aInstance.getId())).uniqueResult();
if (dbB != null) //exists in DB
{
aInstance.getB().setId(aInstance.getId()); //Tell hibernate it is an Update
//Do the same for any other child classes to B with the same strategy if there are any in here
}
else
{
aInstance.getB().setId(null); //Tell hibernate it is a Save
}
unmarshaller.getDetachedSession().clear();
(使用分离 session ,以便主 session 远离不需要的对象,避免“ session 中具有相同标识符的对象
”异常)
如果你不需要 DB 对象,只想知道它是否存在于 DB 中,你可以使用 Count,使它更轻量:
String query = "select count(*) from " + B.class.getName() + " where id = " + aInstance.getId();
Long count = DataAccessUtils.uniqueResult(hibernateTemplate.find(query));
if (count != null && count > 0)
{
aInstance.getB().setId(aInstance.getId()); // update
}
else
{
aInstance.getB().setId(null); // save
}
现在您可以saveOrUpdate(aInstance);
但正如我所说,@MapsId
策略对 Hibernate 不是很友好。
关于java - Hibernate @MapsId 为什么会出现这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37320011/