我有一个架构,我有3个桌子。假设A,B,C。 A是child的父表.B,C是子表。
当我致电 hibernateSession.saveOrUpdate 正在更新所有三个表时。
但是在子表中我没有父表的数据,但在父表中我有数据。所以它给出了异常
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20; // array count
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
// how many cell will be alloc here
}
// configure cell
}
所以我想知道hibernate中是否存在任何方式,以便如果子表没有数据,则在更新父表数据时插入其他更新。
编辑1:
查询打印如下
org.hibernate.StaleStateException: Batch update returned
unexpected row count from update [0]; actual row count: 0; expected: 1.
但我在B和C表中没有记录。
修改:2
因此,如果表B和C没有数据,则插入数据。
这是我的代码。
Update A set name =? where aid = ?
Update B set adder =? where bid = ?
Update C set city =? where cid = ?
但我得到了这个例外。
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);
}
}
}
}
答案 0 :(得分:0)
我建议您在Hibernate.cfg.xml
配置中启用hibernate语句日志记录,以了解发生的情况,因为您不发布表结构和查询。
启用Hibernate语句日志记录:
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
然后请用打印的查询更新您的问题。
更新具有表中不存在的id的对象时会引发此异常。该错误意味着它无法找到您给定ID的记录。
您应该阅读具有相同ID的记录:
update
找不到例外记录。
关于此错误:
Hibernate错误:org.hibernate.NonUniqueObjectException:具有相同标识符值的其他对象已与会话关联
您应该告诉hibernate会话将修改后的对象与会话
中的对象合并session.merge(object)
答案 1 :(得分:0)
您可以在hibernate配置中设置 show_sql = true 。 然后你可以看到抛出异常的update语句。 如果没有看到你的数据类型,说一些东西是不够的, 桌子和关系。