【发布时间】:2016-09-09 07:26:29
【问题描述】:
我有一个架构,我有 3 个表。假设 A、B、C。 A是child的父表,B,C是子表。
当我调用 hibernateSession.saveOrUpdate 它是更新所有三个表。
但是在子表中我没有父表的数据,但在父表中我有数据。所以它给出了异常
org.hibernate.StaleStateException: Batch update returned
unexpected row count from update [0]; actual row count: 0; expected: 1.
所以我想知道休眠中是否有任何方法,以便如果子表没有数据,则在更新父表数据时插入其他更新。
编辑 1:
这样打印的查询
Update A set name =? where aid = ?
Update B set adder =? where bid = ?
Update C set city =? where cid = ?
但我在 B 和 C 表中没有记录。
编辑:2
所以如果表B和C都没有数据则插入数据。
这是我的代码。
A a = session.get(aid);
B b = a.getb();
C c = a.getc();
// Save B if not exists in database
if (b != null) {
if (b.bid() != null) {
if (session.get(b.bid()) == null) {
System.out.println("--record is there in db---");
session.save(b);
}
}
}
// Save C if not exists in database
if (c != null) {
if (c.cid() != null) {
if (session.get(c.cid()) == null) {
System.out
.println("--record is there in db in update---");
session.save(c);
}
}
}
}
但我遇到了这个异常。
Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
【问题讨论】:
标签: java hibernate batch-processing