|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.locking; |
| 6 | + |
| 7 | +import jakarta.persistence.Basic; |
| 8 | +import jakarta.persistence.Column; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.Inheritance; |
| 12 | +import jakarta.persistence.InheritanceType; |
| 13 | +import jakarta.persistence.LockModeType; |
| 14 | +import jakarta.persistence.Version; |
| 15 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 16 | +import org.hibernate.testing.orm.junit.Jira; |
| 17 | +import org.hibernate.testing.orm.junit.Jpa; |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +@Jpa( |
| 25 | + annotatedClasses = { |
| 26 | + JoinedInheritancePessimisticLockingTest.BaseThing.class, |
| 27 | + JoinedInheritancePessimisticLockingTest.ConcreteThing.class, |
| 28 | + JoinedInheritancePessimisticLockingTest.AnotherConcreteThing.class, |
| 29 | + } |
| 30 | +) |
| 31 | +@Jira("https://hibernate.atlassian.net/browse/HHH-7315") |
| 32 | +public class JoinedInheritancePessimisticLockingTest { |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void setup(EntityManagerFactoryScope scope) { |
| 36 | + scope.inTransaction( |
| 37 | + entityManager -> { |
| 38 | + ConcreteThing t1 = new ConcreteThing(); |
| 39 | + t1.id = 1L; |
| 40 | + t1.name = "t1"; |
| 41 | + t1.aProp = "abc"; |
| 42 | + AnotherConcreteThing t2 = new AnotherConcreteThing(); |
| 43 | + t2.id = 2L; |
| 44 | + t2.name = "t2"; |
| 45 | + t2.anotherProp = "def"; |
| 46 | + entityManager.persist( t1 ); |
| 47 | + entityManager.persist( t2 ); |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 54 | + scope.inTransaction( |
| 55 | + entityManager -> |
| 56 | + entityManager.createQuery( "delete from BaseThing" ).executeUpdate() |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void findWithLock(EntityManagerFactoryScope scope) { |
| 62 | + scope.inTransaction(entityManager -> { |
| 63 | + BaseThing t = entityManager.find( BaseThing.class, 1L, LockModeType.PESSIMISTIC_WRITE ); |
| 64 | + assertEquals( LockModeType.PESSIMISTIC_WRITE, entityManager.getLockMode( t ) ); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void findThenLock(EntityManagerFactoryScope scope) { |
| 70 | + scope.inTransaction(entityManager -> { |
| 71 | + BaseThing t = entityManager.find( BaseThing.class, 1L ); |
| 72 | + entityManager.lock( t, LockModeType.PESSIMISTIC_WRITE ); |
| 73 | + assertEquals( LockModeType.PESSIMISTIC_WRITE, entityManager.getLockMode( t ) ); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Entity(name = "BaseThing") |
| 78 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 79 | + public static abstract class BaseThing { |
| 80 | + @Id |
| 81 | + @Column(nullable = false) |
| 82 | + Long id; |
| 83 | + |
| 84 | + @Basic(optional = false) |
| 85 | + @Column(nullable = false) |
| 86 | + String name; |
| 87 | + |
| 88 | + @Version |
| 89 | + @Column(name = "version") |
| 90 | + int version; |
| 91 | + |
| 92 | + } |
| 93 | + @Entity(name = "ConcreteThing") |
| 94 | + public static class ConcreteThing extends BaseThing { |
| 95 | + @Basic(optional = false) |
| 96 | + @Column(nullable = false) |
| 97 | + String aProp; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Entity(name = "AnotherConcreteThing") |
| 102 | + public static class AnotherConcreteThing extends BaseThing { |
| 103 | + @Basic(optional = false) |
| 104 | + @Column(nullable = false) |
| 105 | + String anotherProp; |
| 106 | + } |
| 107 | +} |
0 commit comments