I am using Hibernate entity manager 3.5.1-Final with MS SQL Server 2005 and trying to persist multiple new entities. My entity is annotationally configured thus:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
After calling
entityManager.persist(newEntity)
I do not see the generatedId set, it remains as 0. This causes the following exception when persisting the next new entity:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.y.z.MyEntity#0]
I can get round this by evicting the recently persisted entity from the cache before I persist the next entity, but this is not ideal. What do I need to do to update the object correctly after insert?