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