After a discussion beikov asked me to create a Jira issue: The documentation states the following for the OptimisticLockType:
NONE optimistic locking is disabled even if there is a @Version annotation present
I assumed that means that by default no exception should be thrown when merging a stale object, but it seems the OptimisticLockType.NONE is ignored and a StaleObjectStateException is thrown anyway. I have created a reproducer using your test example and attached it to the issue. Here is the code as well:
package org.hibernate.bugs.entities;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Version;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.OptimisticLocking;
@Entity
@OptimisticLocking(type = OptimisticLockType.NONE)
public class Example {
@Id
@GeneratedValue
private Long id;
private String name;
@Version
private Long rowVersion;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getRowVersion() {
return rowVersion;
}
public void setRowVersion(Long rowVersion) {
this.rowVersion = rowVersion;
}
}
package org.hibernate.bugs;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.hibernate.bugs.entities.Example;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
public class JPAUnitTestCase {
private EntityManagerFactory entityManagerFactory;
@Before
public void init() {
entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" );
}
@After
public void destroy() {
entityManagerFactory.close();
}
@Test
public void hhh123Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Example example = new Example();
example.setName("Example1");
entityManager.persist(example);
Long exampleId = example.getId();
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Example example2 = entityManager.find(Example.class, exampleId);
example2.setName("Example2");
entityManager.merge(example2);
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
example.setName("Example3");
entityManager.merge(example);
entityManager.getTransaction().commit();
entityManager.close();
}
}
|