|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.annotations.naturalid; |
| 6 | + |
| 7 | +import org.hibernate.annotations.NaturalId; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.Id; |
| 18 | + |
| 19 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 20 | + |
| 21 | + |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { |
| 24 | + ByteArrayNaturalIdTest.TestEntity.class, |
| 25 | + } |
| 26 | +) |
| 27 | +@SessionFactory |
| 28 | +@JiraKey("HHH-18409") |
| 29 | +public class ByteArrayNaturalIdTest { |
| 30 | + |
| 31 | + private static final String NATURAL_ID_1 = "N1"; |
| 32 | + |
| 33 | + @AfterEach |
| 34 | + public void tearDown(SessionFactoryScope scope) { |
| 35 | + scope.inTransaction( |
| 36 | + session -> |
| 37 | + session.createMutationQuery( "delete TestEntity" ).executeUpdate() |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testSelectByNaturalId(SessionFactoryScope scope) { |
| 43 | + scope.inTransaction( |
| 44 | + session -> { |
| 45 | + TestEntity entity = new TestEntity( 1L, new byte[] { 1, 2 }, NATURAL_ID_1 ); |
| 46 | + session.persist( entity ); |
| 47 | + TestEntity testEntity = session.byNaturalId( TestEntity.class ) |
| 48 | + .using( "naturalId2", NATURAL_ID_1 ) |
| 49 | + .using( "naturalId1", new byte[] { 1, 2 } ) |
| 50 | + .load(); |
| 51 | + |
| 52 | + assertThat( testEntity ).as( "Loading the entity by its natural id failed" ).isNotNull(); |
| 53 | + TestEntity testEntity2 = session.byNaturalId( TestEntity.class ) |
| 54 | + .using( "naturalId2", NATURAL_ID_1 ) |
| 55 | + .using( "naturalId1", new byte[] { 1, 3 } ) |
| 56 | + .load(); |
| 57 | + assertThat( testEntity2 ).as( "Loading the entity using wrong natural id failed" ).isNull(); |
| 58 | + |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + @Entity(name = "TestEntity") |
| 64 | + public static class TestEntity { |
| 65 | + @Id |
| 66 | + private Long id; |
| 67 | + |
| 68 | + @NaturalId |
| 69 | + private byte[] naturalId1; |
| 70 | + |
| 71 | + @NaturalId |
| 72 | + private String naturalId2; |
| 73 | + |
| 74 | + public TestEntity() { |
| 75 | + } |
| 76 | + |
| 77 | + public TestEntity(Long id, byte[] naturalId1, String naturalId2) { |
| 78 | + this.id = id; |
| 79 | + this.naturalId1 = naturalId1; |
| 80 | + this.naturalId2 = naturalId2; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + } |
| 85 | +} |
0 commit comments