|
| 1 | +package org.hibernate.orm.test.inheritance.discriminator; |
| 2 | + |
| 3 | +import org.hibernate.annotations.JdbcTypeCode; |
| 4 | + |
| 5 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 6 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 7 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import jakarta.persistence.Column; |
| 12 | +import jakarta.persistence.DiscriminatorColumn; |
| 13 | +import jakarta.persistence.DiscriminatorValue; |
| 14 | +import jakarta.persistence.Entity; |
| 15 | +import jakarta.persistence.Id; |
| 16 | +import jakarta.persistence.Inheritance; |
| 17 | +import jakarta.persistence.Table; |
| 18 | + |
| 19 | +import static jakarta.persistence.InheritanceType.SINGLE_TABLE; |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.hibernate.type.SqlTypes.JSON; |
| 22 | + |
| 23 | +@DomainModel( |
| 24 | + annotatedClasses = { |
| 25 | + // the order is important to reproduce the issue |
| 26 | + SingleTableAndGenericsTest.B.class, |
| 27 | + SingleTableAndGenericsTest.A.class, |
| 28 | + } |
| 29 | +) |
| 30 | +@SessionFactory |
| 31 | +@JiraKey("HHH-17644") |
| 32 | +public class SingleTableAndGenericsTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testIt(SessionFactoryScope scope) { |
| 36 | + String payload = "{\"book\": \"1\"}"; |
| 37 | + String aId = "1"; |
| 38 | + |
| 39 | + scope.inTransaction( |
| 40 | + session -> { |
| 41 | + A a = new A(); |
| 42 | + a.setId( aId ); |
| 43 | + session.persist( a ); |
| 44 | + a.setPayload( payload ); |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + scope.inTransaction( |
| 49 | + session -> { |
| 50 | + A a = session.find( A.class, aId ); |
| 51 | + assertThat( a ).isNotNull(); |
| 52 | + String payload1 = a.getPayload(); |
| 53 | + assertThat( payload1 ).isNotNull(); |
| 54 | + assertThat( payload1 ).contains( "book" ); |
| 55 | + } |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Entity(name = "B") |
| 60 | + @Table(name = "table_b") |
| 61 | + @Inheritance(strategy = SINGLE_TABLE) |
| 62 | + @DiscriminatorColumn(name = "demo_type") |
| 63 | + public static abstract class B<T> { |
| 64 | + |
| 65 | + @Id |
| 66 | + @Column(name = "id", nullable = false) |
| 67 | + private String id; |
| 68 | + |
| 69 | + @Column(name = "payload") |
| 70 | + @JdbcTypeCode(JSON) |
| 71 | + private T payload; |
| 72 | + |
| 73 | + public String getId() { |
| 74 | + return id; |
| 75 | + } |
| 76 | + |
| 77 | + public void setId(String id) { |
| 78 | + this.id = id; |
| 79 | + } |
| 80 | + |
| 81 | + public T getPayload() { |
| 82 | + return payload; |
| 83 | + } |
| 84 | + |
| 85 | + public void setPayload(T payload) { |
| 86 | + this.payload = payload; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Entity(name = "C") |
| 91 | + @DiscriminatorValue("child") |
| 92 | + public static class A extends B<String> { |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments