7
7
import jakarta .persistence .Entity ;
8
8
import jakarta .persistence .Id ;
9
9
import jakarta .persistence .Version ;
10
+ import org .hibernate .StaleObjectStateException ;
10
11
import org .hibernate .testing .orm .junit .DomainModel ;
11
12
import org .hibernate .testing .orm .junit .SessionFactory ;
12
13
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
13
14
import org .junit .jupiter .api .Test ;
14
15
15
16
import static org .junit .jupiter .api .Assertions .assertEquals ;
17
+ import static org .junit .jupiter .api .Assertions .fail ;
16
18
17
19
@ SessionFactory
18
20
@ DomainModel (annotatedClasses = UpsertVersionedTest .Record .class )
19
21
public class UpsertVersionedTest {
22
+
20
23
@ Test void test (SessionFactoryScope scope ) {
24
+ scope .getSessionFactory ().getSchemaManager ().truncate ();
21
25
scope .inStatelessTransaction (s -> {
22
26
s .upsert (new Record (123L ,null ,"hello earth" ));
23
27
s .upsert (new Record (456L ,2L ,"hello mars" ));
@@ -41,6 +45,29 @@ public class UpsertVersionedTest {
41
45
assertEquals ( "goodbye mars" , s .get ( Record .class ,456L ).message );
42
46
});
43
47
}
48
+
49
+ @ Test void testStaleUpsert (SessionFactoryScope scope ) {
50
+ scope .getSessionFactory ().getSchemaManager ().truncate ();
51
+ scope .inStatelessTransaction ( s -> {
52
+ s .insert (new Record (789L , 1L , "hello world" ));
53
+ } );
54
+ scope .inStatelessTransaction ( s -> {
55
+ s .upsert (new Record (789L , 1L , "hello mars" ));
56
+ } );
57
+ try {
58
+ scope .inStatelessTransaction ( s -> {
59
+ s .upsert (new Record ( 789L , 1L , "hello venus" ));
60
+ } );
61
+ fail ();
62
+ }
63
+ catch (StaleObjectStateException sose ) {
64
+ //expected
65
+ }
66
+ scope .inStatelessTransaction ( s -> {
67
+ assertEquals ( "hello mars" , s .get (Record .class ,789L ).message );
68
+ } );
69
+ }
70
+
44
71
@ Entity (name = "Record" )
45
72
static class Record {
46
73
@ Id Long id ;
0 commit comments