|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.locking; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.LockModeType; |
| 10 | +import jakarta.persistence.Version; |
| 11 | +import org.hibernate.annotations.CurrentTimestamp; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.time.LocalDateTime; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | + |
| 20 | +@Jpa(annotatedClasses = CurrentTimestampVersionTest.Timestamped.class) |
| 21 | +class CurrentTimestampVersionTest { |
| 22 | + @Test void test(EntityManagerFactoryScope scope) { |
| 23 | + Timestamped t = scope.fromTransaction( entityManager -> { |
| 24 | + Timestamped timestamped = new Timestamped(); |
| 25 | + timestamped.id = 1L; |
| 26 | + entityManager.persist( timestamped ); |
| 27 | + return timestamped; |
| 28 | + } ); |
| 29 | + assertNotNull( t.timestamp ); |
| 30 | + scope.inTransaction( entityManager -> { |
| 31 | + Timestamped timestamped = entityManager.find( Timestamped.class, 1L, |
| 32 | + LockModeType.OPTIMISTIC_FORCE_INCREMENT ); |
| 33 | + assertNotNull( timestamped ); |
| 34 | + assertNotNull( timestamped.timestamp ); |
| 35 | + } ); |
| 36 | + scope.inTransaction( entityManager -> { |
| 37 | + Timestamped timestamped = entityManager.find( Timestamped.class, 1L ); |
| 38 | + timestamped.content = "new content"; |
| 39 | + } ); |
| 40 | + // TODO: assert some stuff about the timestamp values |
| 41 | + } |
| 42 | + @Entity |
| 43 | + static class Timestamped { |
| 44 | + @Id |
| 45 | + Long id; |
| 46 | + @CurrentTimestamp @Version |
| 47 | + LocalDateTime timestamp; |
| 48 | + String content; |
| 49 | + } |
| 50 | +} |
0 commit comments