|
| 1 | +package org.hibernate.orm.test.cache; |
| 2 | + |
| 3 | +import org.hibernate.annotations.Cache; |
| 4 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 5 | +import org.hibernate.annotations.NaturalId; |
| 6 | +import org.hibernate.annotations.NaturalIdCache; |
| 7 | +import org.hibernate.cfg.AvailableSettings; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.hibernate.testing.orm.junit.Setting; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.Table; |
| 20 | + |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | + |
| 23 | + |
| 24 | +@DomainModel(annotatedClasses = { NaturalIdTest.TestEntity.class }) |
| 25 | +@SessionFactory |
| 26 | +@ServiceRegistry(settings = { |
| 27 | + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), |
| 28 | + @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"), |
| 29 | +}) |
| 30 | +@JiraKey("HHH_18511") |
| 31 | +public class NaturalIdTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void testGetNaturalIdentifierSnapshot(SessionFactoryScope scope) { |
| 35 | + final var naturalId1 = "id1"; |
| 36 | + final var naturalId2 = "id2"; |
| 37 | + |
| 38 | + scope.inTransaction( session -> { |
| 39 | + var testEntity = new TestEntity( 1l, naturalId1, naturalId2, "abc" ); |
| 40 | + session.persist( testEntity ); |
| 41 | + session.flush(); |
| 42 | + |
| 43 | + var sessionFactory = session.getFactory(); |
| 44 | + var entityPersister = sessionFactory.getRuntimeMetamodels() |
| 45 | + .getMappingMetamodel() |
| 46 | + .getEntityDescriptor( TestEntity.class ); |
| 47 | + |
| 48 | + Object[] naturalId = (Object[]) entityPersister.getNaturalIdentifierSnapshot( 1l, session ); |
| 49 | + assertThat( naturalId[0] ).isEqualTo( naturalId1 ); |
| 50 | + assertThat( naturalId[1] ).isEqualTo( naturalId2 ); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @Entity(name = "CompositeNaturalIdModel") |
| 55 | + @Table(name = "CompositeNaturalIdModel") |
| 56 | + @NaturalIdCache |
| 57 | + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) |
| 58 | + public static class TestEntity { |
| 59 | + |
| 60 | + @Id |
| 61 | + private Long id; |
| 62 | + |
| 63 | + @NaturalId |
| 64 | + private String naturalId1; |
| 65 | + |
| 66 | + @NaturalId |
| 67 | + private String naturalId2; |
| 68 | + |
| 69 | + private String name; |
| 70 | + |
| 71 | + public TestEntity() { |
| 72 | + } |
| 73 | + |
| 74 | + public TestEntity(Long id, String naturalId1, String naturalId2, String name) { |
| 75 | + this.id = id; |
| 76 | + this.naturalId1 = naturalId1; |
| 77 | + this.naturalId2 = naturalId2; |
| 78 | + this.name = name; |
| 79 | + } |
| 80 | + |
| 81 | + public Long getId() { |
| 82 | + return id; |
| 83 | + } |
| 84 | + |
| 85 | + public String getNaturalId1() { |
| 86 | + return naturalId1; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public String getNaturalId2() { |
| 91 | + return naturalId2; |
| 92 | + } |
| 93 | + |
| 94 | + public String getName() { |
| 95 | + return name; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments