|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.generics.compositeid; |
| 6 | + |
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.IdClass; |
| 11 | +import jakarta.persistence.MappedSuperclass; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import java.io.Serializable; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +@JiraKey("HHH-19706") |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { |
| 24 | + CompositeIdWithGenericPartInMappedSuperclassTest.SampleCompositeIdEntity.class, |
| 25 | + CompositeIdWithGenericPartInMappedSuperclassTest.SampleEntity.class, |
| 26 | + CompositeIdWithGenericPartInMappedSuperclassTest.SampleSuperclass.class |
| 27 | + } |
| 28 | +) |
| 29 | +@SessionFactory |
| 30 | +class CompositeIdWithGenericPartInMappedSuperclassTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void test(SessionFactoryScope scope) { |
| 34 | + scope.inSession( s -> s.createQuery( "from SampleEntity", SampleEntity.class ).getResultList() ); |
| 35 | + } |
| 36 | + |
| 37 | + @Entity |
| 38 | + @IdClass(SampleCompositeIdEntity.AdditionalIdEntityId.class) |
| 39 | + static class SampleCompositeIdEntity extends SampleSuperclass<Long> { |
| 40 | + |
| 41 | + @Id |
| 42 | + @Column(nullable = false, updatable = false) |
| 43 | + private Long additionalId; |
| 44 | + |
| 45 | + static class AdditionalIdEntityId implements Serializable { |
| 46 | + |
| 47 | + final Long mainId; |
| 48 | + final Long additionalId; |
| 49 | + |
| 50 | + public AdditionalIdEntityId() { |
| 51 | + this( 0L, 0L ); |
| 52 | + } |
| 53 | + |
| 54 | + public AdditionalIdEntityId(Long mainId, Long additionalId) { |
| 55 | + this.mainId = mainId; |
| 56 | + this.additionalId = additionalId; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean equals(Object o) { |
| 61 | + if ( this == o ) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + if ( !(o instanceof AdditionalIdEntityId) ) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + AdditionalIdEntityId key = (AdditionalIdEntityId) o; |
| 68 | + return Objects.equals( mainId, key.mainId ) && Objects.equals( additionalId, key.additionalId ); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int hashCode() { |
| 73 | + return Objects.hash( mainId, additionalId ); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Entity(name = "SampleEntity") |
| 81 | + static class SampleEntity extends SampleSuperclass<Long> { |
| 82 | + |
| 83 | + private String sampleField; |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @MappedSuperclass |
| 88 | + abstract static class SampleSuperclass<K extends Serializable & Comparable<K>> { |
| 89 | + |
| 90 | + @Id |
| 91 | + private K mainId; |
| 92 | + |
| 93 | + } |
| 94 | +} |
0 commit comments