77import jakarta .persistence .Entity ;
88import jakarta .persistence .Id ;
99import jakarta .persistence .Version ;
10+ import org .hibernate .StaleObjectStateException ;
1011import org .hibernate .testing .orm .junit .DomainModel ;
1112import org .hibernate .testing .orm .junit .SessionFactory ;
1213import org .hibernate .testing .orm .junit .SessionFactoryScope ;
1314import org .junit .jupiter .api .Test ;
1415
1516import static org .junit .jupiter .api .Assertions .assertEquals ;
17+ import static org .junit .jupiter .api .Assertions .fail ;
1618
1719@ SessionFactory
1820@ DomainModel (annotatedClasses = UpsertVersionedTest .Record .class )
1921public class UpsertVersionedTest {
22+
2023 @ Test void test (SessionFactoryScope scope ) {
24+ scope .getSessionFactory ().getSchemaManager ().truncate ();
2125 scope .inStatelessTransaction (s -> {
2226 s .upsert (new Record (123L ,null ,"hello earth" ));
2327 s .upsert (new Record (456L ,2L ,"hello mars" ));
@@ -41,6 +45,29 @@ public class UpsertVersionedTest {
4145 assertEquals ( "goodbye mars" , s .get ( Record .class ,456L ).message );
4246 });
4347 }
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+
4471 @ Entity (name = "Record" )
4572 static class Record {
4673 @ Id Long id ;
0 commit comments