11
11
import org .hibernate .annotations .Fetch ;
12
12
import org .hibernate .annotations .FetchMode ;
13
13
14
+ import org .junit .After ;
14
15
import org .junit .Test ;
15
16
16
17
import jakarta .persistence .CascadeType ;
@@ -32,6 +33,13 @@ protected Collection<Class<?>> annotatedEntities() {
32
33
return List .of ( Foo .class , Bar .class );
33
34
}
34
35
36
+ @ After
37
+ public void cleanDb (TestContext context ) {
38
+ test ( context , getSessionFactory ()
39
+ .withTransaction ( s -> s .createQuery ( "delete from Foo" ).executeUpdate ()
40
+ .thenCompose ( v -> s .createQuery ( "delete from Bar" ).executeUpdate () ) ) );
41
+ }
42
+
35
43
@ Test
36
44
public void testFindJoin (TestContext context ) {
37
45
Foo foo = new Foo ( new Bar ( "unique" ) );
@@ -41,8 +49,8 @@ public void testFindJoin(TestContext context) {
41
49
.thenAccept ( v -> session .clear () )
42
50
.thenCompose ( v -> session .find ( Foo .class , foo .id ) )
43
51
.thenAccept ( result -> {
44
- context .assertTrue ( Hibernate .isInitialized ( result .bar ) );
45
- context .assertEquals ( "unique" , result .bar . key );
52
+ context .assertTrue ( Hibernate .isInitialized ( result .getBar () ) );
53
+ context .assertEquals ( "unique" , result .getBar (). getKey () );
46
54
} )
47
55
) );
48
56
}
@@ -56,8 +64,8 @@ public void testMergeDetached(TestContext context) {
56
64
.thenCompose ( i -> getSessionFactory ()
57
65
.withTransaction ( session -> session .merge ( new Foo ( bar ) ) ) )
58
66
.thenCompose ( result -> getSessionFactory ()
59
- .withTransaction ( session -> session .fetch ( result .bar )
60
- .thenAccept ( b -> context .assertEquals ( "unique2" , b .key ) )
67
+ .withTransaction ( session -> session .fetch ( result .getBar () )
68
+ .thenAccept ( b -> context .assertEquals ( "unique2" , b .getKey () ) )
61
69
) ) );
62
70
}
63
71
@@ -68,8 +76,8 @@ public void testMergeReference(TestContext context) {
68
76
.withTransaction ( session -> session .persist ( bar ) )
69
77
.thenCompose ( i -> getSessionFactory ()
70
78
.withTransaction ( session -> session .merge ( new Foo ( session .getReference ( Bar .class , bar .id ) )) ) )
71
- .thenCompose ( result -> getSessionFactory ().withTransaction ( session -> session .fetch ( result .bar )
72
- .thenAccept ( b -> context .assertEquals ( "unique3" , b .key ) )
79
+ .thenCompose ( result -> getSessionFactory ().withTransaction ( session -> session .fetch ( result .getBar () )
80
+ .thenAccept ( b -> context .assertEquals ( "unique3" , b .getKey () ) )
73
81
) ) );
74
82
}
75
83
@@ -89,6 +97,22 @@ static class Foo {
89
97
@ Fetch (FetchMode .JOIN )
90
98
@ JoinColumn (name = "bar_key" , referencedColumnName = "nat_key" )
91
99
Bar bar ;
100
+
101
+ public long getId () {
102
+ return id ;
103
+ }
104
+
105
+ public void setId (long id ) {
106
+ this .id = id ;
107
+ }
108
+
109
+ public Bar getBar () {
110
+ return bar ;
111
+ }
112
+
113
+ public void setBar (Bar bar ) {
114
+ this .bar = bar ;
115
+ }
92
116
}
93
117
94
118
@ Entity (name = "Bar" )
@@ -105,5 +129,21 @@ static class Bar implements Serializable {
105
129
long id ;
106
130
@ Column (name = "nat_key" , unique = true )
107
131
String key ;
132
+
133
+ public long getId () {
134
+ return id ;
135
+ }
136
+
137
+ public void setId (long id ) {
138
+ this .id = id ;
139
+ }
140
+
141
+ public String getKey () {
142
+ return key ;
143
+ }
144
+
145
+ public void setKey (String key ) {
146
+ this .key = key ;
147
+ }
108
148
}
109
149
}
0 commit comments