我收到的时候我想改变我的以下异常@ID中的@Entity.
identifier of an instance of com.google.search.pagerank.ItemEntity was altered from 1 to 2.
我知道我正在改变表格中的主键.我正在使用JPA-annotations.
我通过使用这个单个HQL查询解决了这个问题: update Table set name=:newName where name=:oldName
而不是使用更多的OO方法:
beginTransaction();
T e = session.load(...);
e.setName(newName);
session.saveOrUdate(e);
commit();
知道差异是什么吗?
Win*_*ett 11
我无法想象你为什么要那样做.完全没有.你为什么要改变一个实体的身份?您还需要更新指向它的其他表中的所有外键.看起来像是痛苦,没有收获.您可能最好将其作为"业务密钥"(普通财产)并使用更永久的代理密钥.我觉得你这一切都错了,但如果你坚持......
基本上你正在做的是创建一个新客户并删除旧客户,这就是我在Hibernate中完成它的方式.
[伪]
Begin Transaction
// create new customer from old
newC = Session.Load<Customer>(42)
Session.Evict(newC)
newC.Id = 1492
Session.Save(newC)
// update other relationships to point to newC
// ....
// delete old customer
oldC = Session.Load<Customer>(42)
Session.Delete(oldC)
Commit Transaction
但是,您可能最好只在一个普通的单个SQL事务中一次性完成它,并且在任何一种情况下,您都可能面临已经拥有"旧"Customer实例的并行进程,这可能会导致一些错误.
| 归档时间: | 
 | 
| 查看次数: | 23259 次 | 
| 最近记录: |