|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.records; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.Jira; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import jakarta.persistence.Embeddable; |
| 18 | +import jakarta.persistence.Embedded; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.Id; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Marco Belladelli |
| 26 | + */ |
| 27 | +@DomainModel( annotatedClasses = { |
| 28 | + RecordEmbeddedPropertyNamesTest.Fee.class, |
| 29 | + RecordEmbeddedPropertyNamesTest.Getaway.class, |
| 30 | + RecordEmbeddedPropertyNamesTest.Vacation.class, |
| 31 | + RecordEmbeddedPropertyNamesTest.TestFee.class, |
| 32 | + RecordEmbeddedPropertyNamesTest.TestGetaway.class, |
| 33 | + RecordEmbeddedPropertyNamesTest.TestVacation.class, |
| 34 | +} ) |
| 35 | +@SessionFactory |
| 36 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18445" ) |
| 37 | +public class RecordEmbeddedPropertyNamesTest { |
| 38 | + @Test |
| 39 | + public void testFee(SessionFactoryScope scope) { |
| 40 | + scope.inTransaction( session -> { |
| 41 | + final TestFee result = session.find( TestFee.class, 1L ); |
| 42 | + assertThat( result.getFee().issuedA() ).isFalse(); |
| 43 | + assertThat( result.getFee().issuedB() ).isTrue(); |
| 44 | + } ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGetaway(SessionFactoryScope scope) { |
| 49 | + scope.inTransaction( session -> { |
| 50 | + final TestGetaway result = session.find( TestGetaway.class, 1L ); |
| 51 | + assertThat( result.getGetaway().getawayA() ).isEqualTo( "A" ); |
| 52 | + assertThat( result.getGetaway().getawayB() ).isEqualTo( "B" ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testVacation(SessionFactoryScope scope) { |
| 58 | + scope.inTransaction( session -> { |
| 59 | + final TestVacation result = session.find( TestVacation.class, 1L ); |
| 60 | + assertThat( result.getVacation().amount() ).isEqualTo( 7 ); |
| 61 | + assertThat( result.getVacation().issued() ).isTrue(); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeAll |
| 66 | + public void setUp(SessionFactoryScope scope) { |
| 67 | + scope.inTransaction( session -> { |
| 68 | + session.persist( new TestFee( 1L, new Fee( true, false ) ) ); |
| 69 | + session.persist( new TestGetaway( 1L, new Getaway( "B", "A" ) ) ); |
| 70 | + session.persist( new TestVacation( 1L, new Vacation( true, 7 ) ) ); |
| 71 | + } ); |
| 72 | + } |
| 73 | + |
| 74 | + @AfterAll |
| 75 | + public void tearDown(SessionFactoryScope scope) { |
| 76 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 77 | + } |
| 78 | + |
| 79 | + @Embeddable |
| 80 | + public record Fee(Boolean issuedB, Boolean issuedA) { |
| 81 | + } |
| 82 | + |
| 83 | + @Entity( name = "TestFee" ) |
| 84 | + static class TestFee { |
| 85 | + @Id |
| 86 | + private Long id; |
| 87 | + @Embedded |
| 88 | + private Fee fee; |
| 89 | + |
| 90 | + public TestFee() { |
| 91 | + } |
| 92 | + |
| 93 | + public TestFee(Long id, Fee fee) { |
| 94 | + this.id = id; |
| 95 | + this.fee = fee; |
| 96 | + } |
| 97 | + |
| 98 | + public Fee getFee() { |
| 99 | + return fee; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Embeddable |
| 104 | + record Getaway(String getawayB, String getawayA) { |
| 105 | + } |
| 106 | + |
| 107 | + @Entity( name = "TestGetaway" ) |
| 108 | + static class TestGetaway { |
| 109 | + @Id |
| 110 | + private Long id; |
| 111 | + |
| 112 | + @Embedded |
| 113 | + private Getaway getaway; |
| 114 | + |
| 115 | + public TestGetaway() { |
| 116 | + } |
| 117 | + |
| 118 | + public TestGetaway(Long id, Getaway getaway) { |
| 119 | + this.id = id; |
| 120 | + this.getaway = getaway; |
| 121 | + } |
| 122 | + |
| 123 | + public Getaway getGetaway() { |
| 124 | + return getaway; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Embeddable |
| 129 | + record Vacation(Boolean issued, Integer amount) { |
| 130 | + } |
| 131 | + |
| 132 | + @Entity( name = "TestVacation" ) |
| 133 | + static class TestVacation { |
| 134 | + @Id |
| 135 | + private Long id; |
| 136 | + |
| 137 | + @Embedded |
| 138 | + private Vacation vacation; |
| 139 | + |
| 140 | + public TestVacation() { |
| 141 | + } |
| 142 | + |
| 143 | + public TestVacation(Long id, Vacation vacation) { |
| 144 | + this.id = id; |
| 145 | + this.vacation = vacation; |
| 146 | + } |
| 147 | + |
| 148 | + public Vacation getVacation() { |
| 149 | + return vacation; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments