I am trying to save entities whose id getMethod is annotated with:
Code:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
If I use hibernateTemplate.save(...) it persists the object but changes any already set id.
If I use saveOrUpdate I get a stale state exception:
Code:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
I am happy for the system to create id for when the id field (a Long) is null but I want it to leave them alone when they are set - either creating or merging while keeping the existing id. If I was using mapping I would use the annotation unavailable unsaved-value option.
I also have a persisted creationDateTime field annotated with:
Code:
@Temporal (TemporalType.TIMESTAMP)
Might this be creating a side affect? Should I only use this annotation if I want hibernate to use the getter for versioning?
Finally I should mention that the entity I am trying to persist has child entities of the same class and other statically set ids.
Any ideas on how to avoid the exception, allow static ids and still automatic id generation?
Thank you,
Tim Milstead.