-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-7315 Add test for locking with joined inheritance
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...src/test/java/org/hibernate/orm/test/locking/JoinedInheritancePessimisticLockingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |