|
| 1 | +package org.hibernate.orm.test.annotations; |
| 2 | + |
| 3 | +import jakarta.persistence.*; |
| 4 | +import org.hibernate.annotations.CreationTimestamp; |
| 5 | +import org.hibernate.annotations.UpdateTimestamp; |
| 6 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 7 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.time.LocalDateTime; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +@DomainModel( |
| 18 | + annotatedClasses = { |
| 19 | + CreationUpdatedTimestampInEmbeddableTest.Event.class, |
| 20 | + CreationUpdatedTimestampInEmbeddableTest.History.class, |
| 21 | + } |
| 22 | +) |
| 23 | +@SessionFactory |
| 24 | +class CreationUpdatedTimestampInEmbeddableTest { |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void setUp(SessionFactoryScope scope) { |
| 28 | + scope.inTransaction( session -> { |
| 29 | + Event event = new Event(); |
| 30 | + event.id = 1L; |
| 31 | + event.name = "conference"; |
| 32 | + session.persist( event ); |
| 33 | + } ); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterEach |
| 37 | + void tearDown(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( session -> { |
| 39 | + session.createMutationQuery( "delete Event" ).executeUpdate(); |
| 40 | + } ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void test(SessionFactoryScope scope) { |
| 45 | + LocalDateTime created = scope.fromTransaction( session -> { |
| 46 | + Event fruit = session.get( Event.class, 1L ); |
| 47 | + return fruit.history.created; |
| 48 | + } ); |
| 49 | + |
| 50 | + scope.inTransaction( session -> { |
| 51 | + Event event = session.get( Event.class, 1L ); |
| 52 | + event.name = "concert"; |
| 53 | + } ); |
| 54 | + |
| 55 | + scope.inTransaction( session -> { |
| 56 | + Event event = session.get( Event.class, 1L ); |
| 57 | + assertThat( event.history.created ).isEqualTo( created ); |
| 58 | + assertThat( event.history.updated ).isNotEqualTo( created ); |
| 59 | + assertThat( event.name ).isEqualTo( "concert" ); |
| 60 | + } ); |
| 61 | + } |
| 62 | + |
| 63 | + @Entity(name = "Event") |
| 64 | + public static class Event { |
| 65 | + |
| 66 | + @Id |
| 67 | + public Long id; |
| 68 | + |
| 69 | + public String name; |
| 70 | + |
| 71 | + @Embedded |
| 72 | + public History history; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Embeddable |
| 77 | + public static class History { |
| 78 | + @Column |
| 79 | + @CreationTimestamp |
| 80 | + public LocalDateTime created; |
| 81 | + |
| 82 | + @Column |
| 83 | + @UpdateTimestamp |
| 84 | + public LocalDateTime updated; |
| 85 | + } |
| 86 | +} |
0 commit comments