|
| 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.idclass; |
| 6 | + |
| 7 | + |
| 8 | +import java.io.Serializable; |
| 9 | +import java.util.Objects; |
| 10 | +import jakarta.persistence.*; |
| 11 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import static org.hamcrest.core.Is.is; |
| 16 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
| 17 | +import static org.junit.Assert.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Aber Tian |
| 21 | + */ |
| 22 | +@JiraKey("HHH-16054") |
| 23 | +public class JoinedSubclassWithIdClassTest extends BaseCoreFunctionalTestCase { |
| 24 | + |
| 25 | + @Override |
| 26 | + protected Class<?>[] getAnnotatedClasses() { |
| 27 | + return new Class[] { |
| 28 | + BaseEntity.class, |
| 29 | + ConcreteEntity.class |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testJoinedSubClassWithIdClassComposeKey() { |
| 35 | + |
| 36 | + ConcreteEntity entity = new ConcreteEntity(); |
| 37 | + entity.setId( 1L ); |
| 38 | + entity.setDealer( "dealer" ); |
| 39 | + entity.setName( "aber" ); |
| 40 | + entity.setAge( 18 ); |
| 41 | + |
| 42 | + Pk pk = new Pk(); |
| 43 | + pk.id = 1L; |
| 44 | + pk.dealer = "dealer"; |
| 45 | + |
| 46 | + doInHibernate( this::sessionFactory, session -> { |
| 47 | + session.merge( entity ); |
| 48 | + } ); |
| 49 | + |
| 50 | + doInHibernate( this::sessionFactory, session -> { |
| 51 | + entity.setName( "tian" ); |
| 52 | + session.merge( entity ); |
| 53 | + BaseEntity baseEntity = session.find( BaseEntity.class, pk ); |
| 54 | + assertThat( baseEntity.name, is( "tian" ) ); |
| 55 | + } ); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public static class Pk implements Serializable { |
| 60 | + private long id; |
| 61 | + |
| 62 | + private String dealer; |
| 63 | + |
| 64 | + public long getId() { |
| 65 | + return id; |
| 66 | + } |
| 67 | + |
| 68 | + public void setId(long id) { |
| 69 | + this.id = id; |
| 70 | + } |
| 71 | + |
| 72 | + public String getDealer() { |
| 73 | + return dealer; |
| 74 | + } |
| 75 | + |
| 76 | + public void setDealer(String dealer) { |
| 77 | + this.dealer = dealer; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean equals(Object o) { |
| 82 | + if ( this == o ) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + if ( o == null || getClass() != o.getClass() ) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + Pk pk = (Pk) o; |
| 89 | + return id == pk.id && Objects.equals( dealer, pk.dealer ); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int hashCode() { |
| 94 | + return Objects.hash( id, dealer ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Entity(name = "BaseEntity") |
| 99 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 100 | + @IdClass(Pk.class) |
| 101 | + public static abstract class BaseEntity { |
| 102 | + @Id |
| 103 | + private long id; |
| 104 | + |
| 105 | + @Id |
| 106 | + private String dealer; |
| 107 | + |
| 108 | + private String name; |
| 109 | + |
| 110 | + public long getId() { |
| 111 | + return id; |
| 112 | + } |
| 113 | + |
| 114 | + public void setId(long id) { |
| 115 | + this.id = id; |
| 116 | + } |
| 117 | + |
| 118 | + public String getDealer() { |
| 119 | + return dealer; |
| 120 | + } |
| 121 | + |
| 122 | + public void setDealer(String dealer) { |
| 123 | + this.dealer = dealer; |
| 124 | + } |
| 125 | + |
| 126 | + public String getName() { |
| 127 | + return name; |
| 128 | + } |
| 129 | + |
| 130 | + public void setName(String name) { |
| 131 | + this.name = name; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Entity(name = "ConcreteEntity") |
| 136 | + public static class ConcreteEntity extends BaseEntity { |
| 137 | + |
| 138 | + private int age; |
| 139 | + |
| 140 | + public int getAge() { |
| 141 | + return age; |
| 142 | + } |
| 143 | + |
| 144 | + public void setAge(int age) { |
| 145 | + this.age = age; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments