Skip to content

Commit 4cef240

Browse files
quaffgavinking
authored andcommitted
HHH-6951 IdentifiableType.getIdType() should respect @IdClass
Before this commit, `IdentifiableType.getIdType()` returns unexpected `null` if multiple @id present.
1 parent b9f15e8 commit 4cef240

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractIdentifiableType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public SimpleDomainType<?> getIdType() {
184184
if ( idClassAttributes.size() == 1 ) {
185185
return idClassAttributes.iterator().next().getType();
186186
}
187+
else if ( idClassType instanceof SimpleDomainType<?> ) {
188+
return (SimpleDomainType<?>) idClassType;
189+
}
187190
}
188191

189192
return null;

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/metamodel/MixedIdAndIdClassHandling.java

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.hibernate.orm.test.jpa.metamodel;
88

9+
import java.util.Objects;
10+
911
import jakarta.persistence.Column;
1012
import jakarta.persistence.Entity;
1113
import jakarta.persistence.Id;
@@ -14,8 +16,8 @@
1416
import jakarta.persistence.Table;
1517
import jakarta.persistence.metamodel.EntityType;
1618

17-
import org.hibernate.testing.TestForIssue;
1819
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
20+
import org.hibernate.testing.orm.junit.JiraKey;
1921
import org.hibernate.testing.orm.junit.Jpa;
2022

2123
import org.junit.jupiter.api.Test;
@@ -29,14 +31,16 @@
2931
* Ugh
3032
*
3133
* @author Steve Ebersole
34+
* @author Yanming Zhou
3235
*/
3336
@Jpa(annotatedClasses = {
34-
MixedIdAndIdClassHandling.FullTimeEmployee.class
37+
MixedIdAndIdClassHandling.FullTimeEmployee.class,
38+
MixedIdAndIdClassHandling.Person.class
3539
})
3640
public class MixedIdAndIdClassHandling {
3741

3842
@Test
39-
@TestForIssue( jiraKey = "HHH-8533" )
43+
@JiraKey( "HHH-8533" )
4044
public void testAccess(EntityManagerFactoryScope scope) {
4145
EntityType<FullTimeEmployee> entityType = scope.getEntityManagerFactory().getMetamodel().entity( FullTimeEmployee.class );
4246
try {
@@ -50,8 +54,16 @@ public void testAccess(EntityManagerFactoryScope scope) {
5054
assertEquals( 1, entityType.getSupertype().getIdClassAttributes().size() );
5155

5256
assertFalse( entityType.hasSingleIdAttribute() );
57+
}
5358

54-
assertEquals( String.class, entityType.getIdType().getJavaType() );
59+
@Test
60+
@JiraKey( "HHH-6951" )
61+
public void testGetIdType(EntityManagerFactoryScope scope) {
62+
EntityType<FullTimeEmployee> fullTimeEmployeeEntityType = scope.getEntityManagerFactory().getMetamodel().entity( FullTimeEmployee.class );
63+
assertEquals( String.class, fullTimeEmployeeEntityType.getIdType().getJavaType() ); // return single @Id instead of @IdClass
64+
65+
EntityType<Person> personEntityType = scope.getEntityManagerFactory().getMetamodel().entity( Person.class );
66+
assertEquals( PersonId.class, personEntityType.getIdType().getJavaType() ); // return @IdClass instead of null
5567
}
5668

5769
@MappedSuperclass
@@ -90,27 +102,78 @@ public void setId(String id) {
90102
this.id = id;
91103
}
92104

93-
94105
@Override
95-
public boolean equals(Object obj) {
96-
if (obj == null) {
97-
return false;
106+
public boolean equals(Object o) {
107+
if ( this == o ) {
108+
return true;
98109
}
99-
if (getClass() != obj.getClass()) {
110+
if ( !( o instanceof EmployeeId ) ) {
100111
return false;
101112
}
102-
final EmployeeId other = (EmployeeId) obj;
103-
if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
113+
EmployeeId that = ( EmployeeId ) o;
114+
return Objects.equals( id, that.id );
115+
}
116+
117+
@Override
118+
public int hashCode() {
119+
return Objects.hashCode( id );
120+
}
121+
}
122+
123+
@IdClass( PersonId.class )
124+
@Entity( name = "Person" )
125+
@Table( name="PERSON" )
126+
public static class Person {
127+
@Id
128+
private String type;
129+
@Id
130+
private String no;
131+
private String name;
132+
}
133+
134+
public static class PersonId implements java.io.Serializable {
135+
String type;
136+
String no;
137+
138+
public PersonId() {
139+
}
140+
141+
public PersonId(String type, String no) {
142+
this.type = type;
143+
this.no = no;
144+
}
145+
146+
public String getType() {
147+
return type;
148+
}
149+
150+
public void setType(String type) {
151+
this.type = type;
152+
}
153+
154+
public String getNo() {
155+
return no;
156+
}
157+
158+
public void setNo(String no) {
159+
this.no = no;
160+
}
161+
162+
@Override
163+
public boolean equals(Object o) {
164+
if ( this == o ) {
165+
return true;
166+
}
167+
if ( !( o instanceof PersonId ) ) {
104168
return false;
105169
}
106-
return true;
170+
PersonId that = ( PersonId ) o;
171+
return Objects.equals( type, that.type ) && Objects.equals( no, that.no );
107172
}
108173

109174
@Override
110175
public int hashCode() {
111-
int hash = 5;
112-
hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
113-
return hash;
176+
return Objects.hash( type, no );
114177
}
115178
}
116179
}

0 commit comments

Comments
 (0)