【发布时间】:2010-08-17 15:21:24
【问题描述】:
我正在使用带有 MS SQL Server 2005 的 Hibernate 实体管理器 3.5.1-Final,并尝试保留多个新实体。我的实体是这样注释配置的:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
调用后
entityManager.persist(newEntity)
我没有看到 generatedId 集,它保持为 0。这在持久化下一个新实体时会导致以下异常:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.y.z.MyEntity#0]
我可以通过在持久化下一个实体之前从缓存中逐出最近持久化的实体来解决这个问题,但这并不理想。插入后我需要做什么才能正确更新对象?
【问题讨论】:
标签: sql-server-2005 hibernate orm jpa identity