diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/LockOptionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/LockOptionsTest.java new file mode 100644 index 0000000000..96d3607609 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/LockOptionsTest.java @@ -0,0 +1,88 @@ +package org.hibernate.orm.test.jpa.findoptions; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.LockModeType; +import jakarta.persistence.Timeout; +import org.hibernate.HibernateException; +import org.hibernate.annotations.FetchProfile; +import org.hibernate.annotations.FetchProfileOverride; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.hibernate.Hibernate.isInitialized; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; + +@Jpa(annotatedClasses = LockOptionsTest.MyEntity.class) +public class LockOptionsTest { + @Test + void test(EntityManagerFactoryScope scope) { + MyEntity hello = new MyEntity("Hello"); + scope.getEntityManagerFactory() + .runInTransaction(em -> em.persist(hello)); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + MyEntity entity = em.find(MyEntity.class, hello.id); + em.lock(entity, LockModeType.PESSIMISTIC_WRITE, Timeout.seconds(10)); + assertEquals(LockModeType.PESSIMISTIC_WRITE, em.getLockMode(entity)); + assertEquals(hello.name, entity.name); + assertFalse(isInitialized(entity.list)); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.lock(em.find(MyEntity.class, hello.id), LockModeType.OPTIMISTIC); + } + catch (HibernateException he) { + return; + } + fail(); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.lock(em.find(MyEntity.class, hello.id), LockModeType.OPTIMISTIC_FORCE_INCREMENT); + } + catch (HibernateException he) { + return; + } + fail(); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.lock(em.find(MyEntity.class, hello.id), LockModeType.PESSIMISTIC_FORCE_INCREMENT); + } + catch (HibernateException he) { + return; + } + fail(); + }); + } + + @Entity + @FetchProfile(name = "hello world") + public static class MyEntity { + @Id @GeneratedValue long id; + String name; + + @ElementCollection(fetch = FetchType.LAZY) + @FetchProfileOverride(profile = "hello world", + fetch = FetchType.EAGER) + List list; + + public MyEntity(String name) { + this.name = name; + } + MyEntity() { + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/RefreshOptionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/RefreshOptionsTest.java new file mode 100644 index 0000000000..a160d6fe55 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/findoptions/RefreshOptionsTest.java @@ -0,0 +1,91 @@ +package org.hibernate.orm.test.jpa.findoptions; + +import jakarta.persistence.CacheStoreMode; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.LockModeType; +import jakarta.persistence.Timeout; +import org.hibernate.HibernateException; +import org.hibernate.LockMode; +import org.hibernate.annotations.FetchProfile; +import org.hibernate.annotations.FetchProfileOverride; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.hibernate.Hibernate.isInitialized; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; + +@Jpa(annotatedClasses = RefreshOptionsTest.MyEntity.class) +public class RefreshOptionsTest { + @Test + void test(EntityManagerFactoryScope scope) { + MyEntity hello = new MyEntity("Hello"); + scope.getEntityManagerFactory() + .runInTransaction(em -> em.persist(hello)); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + MyEntity entity = em.find(MyEntity.class, hello.id); + em.refresh(entity, LockModeType.PESSIMISTIC_WRITE, Timeout.seconds(10), + CacheStoreMode.BYPASS); + assertEquals(LockModeType.PESSIMISTIC_WRITE, em.getLockMode(entity)); + assertEquals(hello.name, entity.name); + assertFalse(isInitialized(entity.list)); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.refresh(em.find(MyEntity.class, hello.id), LockMode.OPTIMISTIC); + } + catch (HibernateException he) { + return; + } + fail(); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.refresh(em.find(MyEntity.class, hello.id), LockMode.OPTIMISTIC_FORCE_INCREMENT); + } + catch (HibernateException he) { + return; + } + fail(); + }); + scope.getEntityManagerFactory() + .runInTransaction(em -> { + try { + em.refresh(em.find(MyEntity.class, hello.id), LockMode.PESSIMISTIC_FORCE_INCREMENT); + } + catch (HibernateException he) { + return; + } + fail(); + }); + } + + @Entity + @FetchProfile(name = "hello world") + public static class MyEntity { + @Id @GeneratedValue long id; + String name; + + @ElementCollection(fetch = FetchType.LAZY) + @FetchProfileOverride(profile = "hello world", + fetch = FetchType.EAGER) + List list; + + public MyEntity(String name) { + this.name = name; + } + MyEntity() { + } + } +}