MK (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNWFmYWU2NjMy...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16095?atlOrigin=eyJpIjoiNWFmYW...
) HHH-16095 (
https://hibernate.atlassian.net/browse/HHH-16095?atlOrigin=eyJpIjoiNWFmYW...
) OptimisticLockType.NONE ignored (
https://hibernate.atlassian.net/browse/HHH-16095?atlOrigin=eyJpIjoiNWFmYW...
)
Issue Type: Bug Affects Versions: 6.1.6, 5.6.14 Assignee: Unassigned Attachments:
hibernate-test-case-optimistic-locking.zip Components: hibernate-core Created: 25/Jan/2023
02:24 AM Priority: Major Reporter: MK (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
After a discussion (
https://discourse.hibernate.org/t/optimisticlocktype-none-ignored-or-not-...
) 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();
}
// Entities are auto-discovered, so just add them anywhere on class- path
// Add your tests, using standard JUnit.
@Test
public void hhh123Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
// Transaction 1 - create entity
entityManager.getTransaction().begin();
// Create new entity
Example example = new Example();
example.setName( "Example1" );
entityManager.persist(example);
Long exampleId = example.getId();
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
// Transaction 2 - update entity
entityManager.getTransaction().begin();
Example example2 = entityManager.find(Example.class, exampleId);
example2.setName( "Example2" );
entityManager.merge(example2);
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
// Transaction 3 - update stale entity
entityManager.getTransaction().begin();
example.setName( "Example3" );
// This should not throw a StaleObjectStateException but does
entityManager.merge(example);
entityManager.getTransaction().commit();
entityManager.close();
}
}
(
https://hibernate.atlassian.net/browse/HHH-16095#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16095#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100214- sha1:3d33cb1 )