From acdbc07eb22497284d407fa1ee2d220400676399 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 14 Jan 2025 19:31:16 +0100 Subject: [PATCH] HHH-7315 Add test for locking with joined inheritance --- ...inedInheritancePessimisticLockingTest.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/locking/JoinedInheritancePessimisticLockingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/locking/JoinedInheritancePessimisticLockingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/locking/JoinedInheritancePessimisticLockingTest.java new file mode 100644 index 000000000000..eba4629f2d13 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/locking/JoinedInheritancePessimisticLockingTest.java @@ -0,0 +1,107 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.locking; + +import jakarta.persistence.Basic; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.LockModeType; +import jakarta.persistence.Version; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Jpa( + annotatedClasses = { + JoinedInheritancePessimisticLockingTest.BaseThing.class, + JoinedInheritancePessimisticLockingTest.ConcreteThing.class, + JoinedInheritancePessimisticLockingTest.AnotherConcreteThing.class, + } +) +@Jira("https://hibernate.atlassian.net/browse/HHH-7315") +public class JoinedInheritancePessimisticLockingTest { + + @BeforeEach + public void setup(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + ConcreteThing t1 = new ConcreteThing(); + t1.id = 1L; + t1.name = "t1"; + t1.aProp = "abc"; + AnotherConcreteThing t2 = new AnotherConcreteThing(); + t2.id = 2L; + t2.name = "t2"; + t2.anotherProp = "def"; + entityManager.persist( t1 ); + entityManager.persist( t2 ); + } + ); + } + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> + entityManager.createQuery( "delete from BaseThing" ).executeUpdate() + ); + } + + @Test + public void findWithLock(EntityManagerFactoryScope scope) { + scope.inTransaction(entityManager -> { + BaseThing t = entityManager.find( BaseThing.class, 1L, LockModeType.PESSIMISTIC_WRITE ); + assertEquals( LockModeType.PESSIMISTIC_WRITE, entityManager.getLockMode( t ) ); + }); + } + + @Test + public void findThenLock(EntityManagerFactoryScope scope) { + scope.inTransaction(entityManager -> { + BaseThing t = entityManager.find( BaseThing.class, 1L ); + entityManager.lock( t, LockModeType.PESSIMISTIC_WRITE ); + assertEquals( LockModeType.PESSIMISTIC_WRITE, entityManager.getLockMode( t ) ); + }); + } + + @Entity(name = "BaseThing") + @Inheritance(strategy = InheritanceType.JOINED) + public static abstract class BaseThing { + @Id + @Column(nullable = false) + Long id; + + @Basic(optional = false) + @Column(nullable = false) + String name; + + @Version + @Column(name = "version") + int version; + + } + @Entity(name = "ConcreteThing") + public static class ConcreteThing extends BaseThing { + @Basic(optional = false) + @Column(nullable = false) + String aProp; + } + + + @Entity(name = "AnotherConcreteThing") + public static class AnotherConcreteThing extends BaseThing { + @Basic(optional = false) + @Column(nullable = false) + String anotherProp; + } +}