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