6
6
*/
7
7
package org .hibernate .orm .test .jpa .metamodel ;
8
8
9
+ import java .util .Objects ;
10
+
9
11
import jakarta .persistence .Column ;
10
12
import jakarta .persistence .Entity ;
11
13
import jakarta .persistence .Id ;
14
16
import jakarta .persistence .Table ;
15
17
import jakarta .persistence .metamodel .EntityType ;
16
18
17
- import org .hibernate .testing .TestForIssue ;
18
19
import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
20
+ import org .hibernate .testing .orm .junit .JiraKey ;
19
21
import org .hibernate .testing .orm .junit .Jpa ;
20
22
21
23
import org .junit .jupiter .api .Test ;
29
31
* Ugh
30
32
*
31
33
* @author Steve Ebersole
34
+ * @author Yanming Zhou
32
35
*/
33
36
@ Jpa (annotatedClasses = {
34
- MixedIdAndIdClassHandling .FullTimeEmployee .class
37
+ MixedIdAndIdClassHandling .FullTimeEmployee .class ,
38
+ MixedIdAndIdClassHandling .Person .class
35
39
})
36
40
public class MixedIdAndIdClassHandling {
37
41
38
42
@ Test
39
- @ TestForIssue ( jiraKey = "HHH-8533" )
43
+ @ JiraKey ( "HHH-8533" )
40
44
public void testAccess (EntityManagerFactoryScope scope ) {
41
45
EntityType <FullTimeEmployee > entityType = scope .getEntityManagerFactory ().getMetamodel ().entity ( FullTimeEmployee .class );
42
46
try {
@@ -50,8 +54,16 @@ public void testAccess(EntityManagerFactoryScope scope) {
50
54
assertEquals ( 1 , entityType .getSupertype ().getIdClassAttributes ().size () );
51
55
52
56
assertFalse ( entityType .hasSingleIdAttribute () );
57
+ }
53
58
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
55
67
}
56
68
57
69
@ MappedSuperclass
@@ -90,27 +102,78 @@ public void setId(String id) {
90
102
this .id = id ;
91
103
}
92
104
93
-
94
105
@ 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 ;
98
109
}
99
- if (getClass () != obj . getClass () ) {
110
+ if ( !( o instanceof EmployeeId ) ) {
100
111
return false ;
101
112
}
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 ) ) {
104
168
return false ;
105
169
}
106
- return true ;
170
+ PersonId that = ( PersonId ) o ;
171
+ return Objects .equals ( type , that .type ) && Objects .equals ( no , that .no );
107
172
}
108
173
109
174
@ Override
110
175
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 );
114
177
}
115
178
}
116
179
}
0 commit comments