Using Hibernate 3 and JPA, I am attempting to merge a transient Java object with an existing entity, but I am getting a StaleObjectStateException.
Imagine for example an Employee entity:
Code:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
.
.
.
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
Let's say I have an Employee entity in my database with id=100 and name="John." Then let's say I do this:
Code:
Employee e = new Employee();
e.setId(100);
e.setName("Mike"):
entityManager.merge(e);
It is basically this sort of situation that is leading to the StaleObjectStateException I mentioned earlier. I assumed that Hibernate would realize there is an existing entity with id=100 and do a SQL UPDATE call on the entity to change "John" to "Mike." Clearly, I am mistaken.
Can you please tell me how I can persist the transient object? I appreciate any insight.
Thanks.