|
| 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.annotations.embeddables.generics; |
| 8 | + |
| 9 | +import org.hibernate.annotations.CompositeType; |
| 10 | +import org.hibernate.orm.test.mapping.embeddable.strategy.usertype.embedded.Name; |
| 11 | +import org.hibernate.orm.test.mapping.embeddable.strategy.usertype.embedded.NameCompositeUserType; |
| 12 | + |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.Jira; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import jakarta.persistence.Embeddable; |
| 22 | +import jakarta.persistence.Embedded; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.MappedSuperclass; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Marco Belladelli |
| 31 | + */ |
| 32 | +@DomainModel( annotatedClasses = { |
| 33 | + CompositeUserTypeInGenericSuperclassTest.GenericSuperclass.class, |
| 34 | + CompositeUserTypeInGenericSuperclassTest.TestEmbeddable.class, |
| 35 | + CompositeUserTypeInGenericSuperclassTest.TestEntity.class, |
| 36 | +} ) |
| 37 | +@SessionFactory |
| 38 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17916" ) |
| 39 | +public class CompositeUserTypeInGenericSuperclassTest { |
| 40 | + @Test |
| 41 | + public void testFind(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( session -> { |
| 43 | + final TestEntity result = session.find( TestEntity.class, 1L ); |
| 44 | + assertThat( result.getGenericEmbedded().getTestProp() ).isEqualTo( 1 ); |
| 45 | + assertThat( result.getName().firstName() ).isEqualTo( "Marco" ); |
| 46 | + assertThat( result.getName().lastName() ).isEqualTo( "Belladelli" ); |
| 47 | + } ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testQuery(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( session -> { |
| 53 | + final TestEntity result = session.createQuery( |
| 54 | + "from TestEntity where genericEmbedded.testProp = 2", |
| 55 | + TestEntity.class |
| 56 | + ).getSingleResult(); |
| 57 | + assertThat( result.getGenericEmbedded().getTestProp() ).isEqualTo( 2 ); |
| 58 | + assertThat( result.getName().firstName() ).isEqualTo( "Andrea" ); |
| 59 | + assertThat( result.getName().lastName() ).isEqualTo( "Boriero" ); |
| 60 | + } ); |
| 61 | + } |
| 62 | + |
| 63 | + @BeforeAll |
| 64 | + public void setUp(SessionFactoryScope scope) { |
| 65 | + scope.inTransaction( session -> { |
| 66 | + session.persist( new TestEntity( 1L, new Name( "Marco", "Belladelli" ), new TestEmbeddable( 1 ) ) ); |
| 67 | + session.persist( new TestEntity( 2L, new Name( "Andrea", "Boriero" ), new TestEmbeddable( 2 ) ) ); |
| 68 | + } ); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterAll |
| 72 | + public void tearDown(SessionFactoryScope scope) { |
| 73 | + scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() ); |
| 74 | + } |
| 75 | + |
| 76 | + @MappedSuperclass |
| 77 | + static class GenericSuperclass<T> { |
| 78 | + @Embedded |
| 79 | + @CompositeType( NameCompositeUserType.class ) |
| 80 | + private Name name; |
| 81 | + |
| 82 | + @Embedded |
| 83 | + T genericEmbedded; |
| 84 | + |
| 85 | + public GenericSuperclass() { |
| 86 | + } |
| 87 | + |
| 88 | + public GenericSuperclass(Name name, T genericEmbedded) { |
| 89 | + this.name = name; |
| 90 | + this.genericEmbedded = genericEmbedded; |
| 91 | + } |
| 92 | + |
| 93 | + public Name getName() { |
| 94 | + return name; |
| 95 | + } |
| 96 | + |
| 97 | + public T getGenericEmbedded() { |
| 98 | + return genericEmbedded; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Embeddable |
| 103 | + static class TestEmbeddable { |
| 104 | + private Integer testProp; |
| 105 | + |
| 106 | + public TestEmbeddable() { |
| 107 | + } |
| 108 | + |
| 109 | + public TestEmbeddable(Integer testProp) { |
| 110 | + this.testProp = testProp; |
| 111 | + } |
| 112 | + |
| 113 | + public Integer getTestProp() { |
| 114 | + return testProp; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Entity( name = "TestEntity" ) |
| 119 | + static class TestEntity extends GenericSuperclass<TestEmbeddable> { |
| 120 | + @Id |
| 121 | + private Long id; |
| 122 | + |
| 123 | + public TestEntity() { |
| 124 | + } |
| 125 | + |
| 126 | + public TestEntity(Long id, Name name, TestEmbeddable genericEmbedded) { |
| 127 | + super( name, genericEmbedded ); |
| 128 | + this.id = id; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments