smile
smile

Reputation: 11

Can not update a JPA entity after persist?

i use Hibernate 4.1.10.Final as jpa provider (with spring container), and i try to update a jpa entity after persist it, but every time i get an org.hibernate.StaleObjectStateException with message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): ... my code is very simple:

@Transactional
public void test() {
  TestEntity e = new TestEntity();
  e.setName("test");
  ......
  em.persist(e);
  ......
  e.setComment("memo...");
}

Is there any thing wrong? Thank you very much for your help.

Upvotes: 1

Views: 494

Answers (2)

user5842
user5842

Reputation: 33

Ran into the same issue - using JPA 2.0 (Hibernate 4.2.4.Final/Spring 3.2.8.RELEASE) Only way around so far has been to detach the entity, find it again, then update it e.g.

  e = new E();  // assume E has @ID int id;
  em.persist(e);
  em.detach(e);
  e = em.find(e.getId());
  e.setFoo('foo');
  ..

Above work but its a hack. At least it can be rolled back

Upvotes: 1

893
893

Reputation: 195

We ran into the same issue, and the solution that we found is to put the creation of the entity in another transaction, but the problem is that we cannot rollback the creation :-(

If someone have a better solution to propose, I am interested too!

Upvotes: 0

Related Questions