4
4
*/
5
5
package org .hibernate .orm .test .locking ;
6
6
7
+ import jakarta .persistence .Entity ;
8
+ import jakarta .persistence .FetchType ;
9
+ import jakarta .persistence .Id ;
10
+ import jakarta .persistence .ManyToOne ;
11
+ import jakarta .persistence .Table ;
12
+ import jakarta .persistence .Version ;
7
13
import org .hibernate .Hibernate ;
8
-
9
14
import org .hibernate .testing .bytecode .enhancement .extension .BytecodeEnhanced ;
10
15
import org .hibernate .testing .orm .junit .DomainModel ;
11
16
import org .hibernate .testing .orm .junit .Jira ;
15
20
import org .junit .jupiter .api .BeforeEach ;
16
21
import org .junit .jupiter .api .Test ;
17
22
18
- import jakarta .persistence .Entity ;
19
- import jakarta .persistence .FetchType ;
20
- import jakarta .persistence .Id ;
21
- import jakarta .persistence .LockModeType ;
22
- import jakarta .persistence .ManyToOne ;
23
- import jakarta .persistence .Version ;
23
+ import static jakarta .persistence .LockModeType .PESSIMISTIC_FORCE_INCREMENT ;
24
+ import static jakarta .persistence .LockModeType .PESSIMISTIC_WRITE ;
25
+ import static org .assertj .core .api .Assertions .assertThat ;
24
26
25
- import static org .junit .Assert .assertEquals ;
26
- import static org .junit .Assert .assertFalse ;
27
- import static org .junit .Assert .assertSame ;
28
- import static org .junit .Assert .assertTrue ;
29
-
30
- @ DomainModel (
31
- annotatedClasses = {
32
- LockExistingBytecodeProxyTest .MainEntity .class ,
33
- LockExistingBytecodeProxyTest .ReferencedEntity .class ,
34
- }
35
- )
27
+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
28
+ @ DomainModel ( annotatedClasses = {
29
+ LockExistingBytecodeProxyTest .Book .class ,
30
+ LockExistingBytecodeProxyTest .Page .class
31
+ })
36
32
@ SessionFactory
37
33
@ BytecodeEnhanced
38
34
@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17828" )
@@ -41,84 +37,77 @@ public class LockExistingBytecodeProxyTest {
41
37
@ Test
42
38
public void testFindAndLockAfterFind (SessionFactoryScope scope ) {
43
39
scope .inTransaction ( session -> {
44
- final MainEntity main = session .find ( MainEntity .class , 1L );
45
- assertFalse ( Hibernate .isInitialized ( main .referenced ) );
46
- final ReferencedEntity lazyEntity = session .find ( ReferencedEntity .class , 1L , LockModeType .PESSIMISTIC_WRITE );
47
- assertEquals ( LockModeType .PESSIMISTIC_WRITE , session .getLockMode ( lazyEntity ) );
48
- assertTrue ( Hibernate .isInitialized ( main .referenced ) );
49
- assertSame ( lazyEntity , main .referenced );
40
+ final Page page = session .find ( Page .class , 1 );
41
+ assertThat ( Hibernate .isInitialized ( page .book ) ).isFalse ();
42
+
43
+ final Book book = session .find ( Book .class , 1 , PESSIMISTIC_WRITE );
44
+ assertThat ( book ).isSameAs ( page .book );
45
+ assertThat ( session .getLockMode ( book ) ).isEqualTo ( PESSIMISTIC_WRITE );
46
+ assertThat ( Hibernate .isInitialized ( book ) ).isTrue ();
50
47
} );
51
48
}
52
49
53
50
@ Test
54
51
public void testLockAfterFind (SessionFactoryScope scope ) {
55
52
scope .inTransaction ( session -> {
56
- final MainEntity main = session .find ( MainEntity .class , 1L );
57
- assertFalse ( Hibernate .isInitialized ( main .referenced ) );
58
- session .lock ( main .referenced , LockModeType .PESSIMISTIC_FORCE_INCREMENT );
59
- assertEquals ( LockModeType .PESSIMISTIC_FORCE_INCREMENT , session .getLockMode ( main .referenced ) );
60
- assertTrue ( Hibernate .isInitialized ( main .referenced ) );
53
+ final Page page = session .find ( Page .class , 1 );
54
+ assertThat ( Hibernate .isInitialized ( page .book ) ).isFalse ();
55
+
56
+ session .lock ( page .book , PESSIMISTIC_FORCE_INCREMENT );
57
+ assertThat ( session .getLockMode ( page .book ) ).isEqualTo ( PESSIMISTIC_FORCE_INCREMENT );
58
+ assertThat ( Hibernate .isInitialized ( page .book ) ).isTrue ();
61
59
} );
62
60
}
63
61
64
62
@ BeforeEach
65
- public void setUp (SessionFactoryScope scope ) {
63
+ public void createTestData (SessionFactoryScope scope ) {
66
64
scope .inTransaction ( session -> {
67
- final ReferencedEntity e1 = new ReferencedEntity ( 1L , "referenced" );
68
- session .persist ( e1 );
69
- session .persist ( new MainEntity ( 1L , e1 ) );
65
+ final Book book = new Book ( 1 , "My Story" );
66
+ final Page page = new Page ( 1 , book );
67
+ session .persist ( book );
68
+ session .persist ( page );
70
69
} );
71
70
}
72
71
73
72
@ AfterEach
74
- public void tearDown (SessionFactoryScope scope ) {
75
- scope .inTransaction ( session -> {
76
- session .createMutationQuery ( "delete from MainEntity" ).executeUpdate ();
77
- session .createMutationQuery ( "delete from ReferencedEntity" ).executeUpdate ();
78
- } );
73
+ public void dropTestData (SessionFactoryScope scope ) {
74
+ scope .dropData ();
79
75
}
80
76
81
- @ Entity ( name = "MainEntity" )
82
- public static class MainEntity {
77
+ @ Entity (name ="Book" )
78
+ @ Table (name ="books" )
79
+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
80
+ public static class Book {
83
81
@ Id
84
- private Long id ;
85
-
86
- @ ManyToOne ( fetch = FetchType . LAZY )
87
- private ReferencedEntity referenced ;
82
+ private Integer id ;
83
+ private String name ;
84
+ @ Version
85
+ private Long version ;
88
86
89
- protected MainEntity () {
87
+ public Book () {
90
88
}
91
89
92
- public MainEntity ( long id , ReferencedEntity referenced ) {
90
+ public Book ( Integer id , String name ) {
93
91
this .id = id ;
94
- this .referenced = referenced ;
95
- }
96
-
97
- public ReferencedEntity getReferenced () {
98
- return referenced ;
92
+ this .name = name ;
99
93
}
100
94
}
101
95
102
- @ Entity ( name = "ReferencedEntity" )
103
- public static class ReferencedEntity {
96
+ @ Entity (name ="Page" )
97
+ @ Table (name ="pages" )
98
+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
99
+ public static class Page {
104
100
@ Id
105
- private Long id ;
106
-
107
- @ Version
108
- private Long version ;
109
-
110
- private String name ;
101
+ private Integer id ;
102
+ @ ManyToOne ( fetch = FetchType .LAZY )
103
+ private Book book ;
111
104
112
- protected ReferencedEntity () {
105
+ public Page () {
113
106
}
114
107
115
- public ReferencedEntity ( long id , String name ) {
108
+ public Page ( Integer id , Book book ) {
116
109
this .id = id ;
117
- this .name = name ;
118
- }
119
-
120
- public long getVersion () {
121
- return version ;
110
+ this .book = book ;
122
111
}
123
112
}
124
113
}
0 commit comments