Environment:
I have that User entity :
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
@Version
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "VERSION", length = 19)
private Date version;
@Column(nullable = false, length = 20)
private String login;
// Getters and Setters
}
I have a search page which lists users, then I click on a user to edit it (giving its userId in the URL).
In the edit form, I store on the server the fields of that entity and when I save my User I do this :
User user = entityManager.find(User.class, userId)
user.setLogin(form.getLogin());
user.setVersion(form.getVersion());
user.setUserId(form.getUserId());
entityManager.merge(user);
Question:
So if I correctly understood optimistic locking with Hibernate, if I open 2 tabs in my browser to edit the same user, then update the login on the first tab, and then the login on the second tab, I should have an OptimisticLockException shouldn’t I ?
Actually, this is not the case on my application… I verified, the form.getVersion() return the same value in both case, even if that in the second update, the user.version has been updated by the first edit.
Am I missing something ?
The EntityManager is produced @RequestScoped (so I’m on two different EntityManagers when I try to merge…).
I’ve tried to do a entityManager.lock(user, LockModeType.OPTIMISTIC_FORCE_INCREMENT) before entityManager.merge(...) (as said here), but it didn’t help.
I’m using Seam 3 with JBoss 7.0.2.Final (which uses Hibernate 4).
Actually I’ve found a way to do that… but I think it’s not really efficient (because there is 1 more SELECT request).
With
entityManager.detach(user), hibernate now uses my settedversionvalue instead of its own copied somewhere value…