22 * SPDX-License-Identifier: Apache-2.0
33 * Copyright Red Hat Inc. and Hibernate Authors
44 */
5- package org .hibernate .metamodel .model .domain ;
5+ package org .hibernate .metamodel .model .domain . internal ;
66
77import java .io .Serializable ;
88import java .util .ArrayList ;
1818import org .hibernate .AssertionFailure ;
1919import org .hibernate .metamodel .UnsupportedMappingException ;
2020import org .hibernate .metamodel .mapping .EntityIdentifierMapping ;
21- import org .hibernate .metamodel .model .domain .internal .AbstractSqmPathSource ;
22- import org .hibernate .metamodel .model .domain .internal .BasicSqmPathSource ;
23- import org .hibernate .metamodel .model .domain .internal .EmbeddedSqmPathSource ;
24- import org .hibernate .metamodel .model .domain .internal .NonAggregatedCompositeSqmPathSource ;
21+ import org .hibernate .metamodel .model .domain .BasicDomainType ;
22+ import org .hibernate .metamodel .model .domain .DomainType ;
23+ import org .hibernate .metamodel .model .domain .EmbeddableDomainType ;
24+ import org .hibernate .metamodel .model .domain .IdentifiableDomainType ;
25+ import org .hibernate .metamodel .model .domain .PersistentAttribute ;
26+ import org .hibernate .metamodel .model .domain .SimpleDomainType ;
27+ import org .hibernate .metamodel .model .domain .SingularPersistentAttribute ;
2528import org .hibernate .metamodel .model .domain .spi .JpaMetamodelImplementor ;
29+ import org .hibernate .query .sqm .SqmPathSource ;
2630import org .hibernate .query .sqm .tree .domain .SqmSingularPersistentAttribute ;
2731import org .hibernate .type .descriptor .java .JavaType ;
2832import org .hibernate .type .descriptor .java .spi .PrimitiveJavaType ;
@@ -54,7 +58,7 @@ public abstract class AbstractIdentifiableType<J>
5458 private Set <SingularPersistentAttribute <? super J ,?>> nonAggregatedIdAttributes ;
5559 private EmbeddableDomainType <?> idClassType ;
5660
57- private PathSource <?> identifierDescriptor ;
61+ private SqmPathSource <?> identifierDescriptor ;
5862
5963 private final boolean isVersioned ;
6064 private SingularPersistentAttribute <J , ?> versionAttribute ;
@@ -80,7 +84,7 @@ protected InFlightAccess<J> createInFlightAccess() {
8084 }
8185
8286 @ Override
83- public PathSource <?> getIdentifierDescriptor () {
87+ public SqmPathSource <?> getIdentifierDescriptor () {
8488 return identifierDescriptor ;
8589 }
8690
@@ -94,9 +98,9 @@ public boolean hasSingleIdAttribute() {
9498 }
9599
96100 @ Override
97- public IdentifiableDomainType <? super J > getSuperType () {
101+ public AbstractIdentifiableType <? super J > getSuperType () {
98102 // overridden simply to perform the cast
99- return (IdentifiableDomainType <? super J >) super .getSuperType ();
103+ return (AbstractIdentifiableType <? super J >) super .getSuperType ();
100104 }
101105
102106 @ Override
@@ -108,7 +112,7 @@ public IdentifiableDomainType<? super J> getSupertype() {
108112 @ SuppressWarnings ("unchecked" )
109113 public <Y > SingularPersistentAttribute <? super J , Y > getId (Class <Y > javaType ) {
110114 ensureNoIdClass ();
111- SingularPersistentAttribute <? super J , ?> id = findIdAttribute ();
115+ final var id = findIdAttribute ();
112116 if ( id != null ) {
113117 checkType ( id , javaType );
114118 }
@@ -168,22 +172,22 @@ public <Y> SingularPersistentAttribute<J, Y> getDeclaredId(Class<Y> javaType) {
168172
169173 @ Override
170174 public SimpleDomainType <?> getIdType () {
171- final SingularPersistentAttribute <? super J , ?> id = findIdAttribute ();
175+ final var id = findIdAttribute ();
172176 if ( id != null ) {
173177 return id .getType ();
174178 }
175-
176- Set <SingularPersistentAttribute <? super J , ?>> idClassAttributes = getIdClassAttributesSafely ();
177- if ( idClassAttributes != null ) {
178- if ( idClassAttributes .size () == 1 ) {
179- return idClassAttributes .iterator ().next ().getType ();
180- }
181- else if ( idClassType instanceof SimpleDomainType <?> simpleDomainType ) {
182- return simpleDomainType ;
179+ else {
180+ final var idClassAttributes = getIdClassAttributesSafely ();
181+ if ( idClassAttributes != null ) {
182+ if ( idClassAttributes .size () == 1 ) {
183+ return idClassAttributes .iterator ().next ().getType ();
184+ }
185+ else if ( idClassType instanceof SimpleDomainType <?> simpleDomainType ) {
186+ return simpleDomainType ;
187+ }
183188 }
189+ return null ;
184190 }
185-
186- return null ;
187191 }
188192
189193 /**
@@ -192,17 +196,14 @@ else if ( idClassType instanceof SimpleDomainType<?> simpleDomainType ) {
192196 * @return IdClass attributes or {@code null}
193197 */
194198 public Set <SingularPersistentAttribute <? super J , ?>> getIdClassAttributesSafely () {
195- if ( !hasIdClass () ) {
196- return null ;
199+ if ( hasIdClass () ) {
200+ final Set <SingularPersistentAttribute <? super J , ?>> attributes = new HashSet <>();
201+ visitIdClassAttributes ( attributes ::add );
202+ return attributes .isEmpty () ? null : attributes ;
197203 }
198- final Set <SingularPersistentAttribute <? super J ,?>> attributes = new HashSet <>();
199- visitIdClassAttributes ( attributes ::add );
200-
201- if ( attributes .isEmpty () ) {
204+ else {
202205 return null ;
203206 }
204-
205- return attributes ;
206207 }
207208
208209 @ Override
@@ -213,11 +214,9 @@ else if ( idClassType instanceof SimpleDomainType<?> simpleDomainType ) {
213214
214215 final Set <SingularAttribute <? super J , ?>> attributes = new HashSet <>();
215216 visitIdClassAttributes ( attributes ::add );
216-
217217 if ( attributes .isEmpty () ) {
218218 throw new IllegalArgumentException ( "Unable to locate IdClass attributes [" + getJavaType () + "]" );
219219 }
220-
221220 return attributes ;
222221 }
223222
@@ -245,41 +244,42 @@ public boolean hasDeclaredVersionAttribute() {
245244 @ Override
246245 @ SuppressWarnings ("unchecked" )
247246 public <Y > SingularPersistentAttribute <? super J , Y > getVersion (Class <Y > javaType ) {
248- if ( ! hasVersionAttribute () ) {
249- return null ;
247+ if ( hasVersionAttribute () ) {
248+ final var version = findVersionAttribute ();
249+ if ( version != null ) {
250+ checkType ( version , javaType );
251+ }
252+ return (SingularPersistentAttribute <? super J , Y >) version ;
250253 }
251-
252- SingularPersistentAttribute <? super J , ?> version = findVersionAttribute ();
253- if ( version != null ) {
254- checkType ( version , javaType );
254+ else {
255+ return null ;
255256 }
256- return (SingularPersistentAttribute <? super J , Y >) version ;
257257 }
258258
259259 @ Override
260260 public SingularPersistentAttribute <? super J , ?> findVersionAttribute () {
261261 if ( versionAttribute != null ) {
262262 return versionAttribute ;
263263 }
264-
265- if ( getSuperType () != null ) {
264+ else if ( getSuperType () != null ) {
266265 return getSuperType ().findVersionAttribute ();
267266 }
268-
269- return null ;
267+ else {
268+ return null ;
269+ }
270270 }
271271
272272 @ Override
273273 public List <? extends PersistentAttribute <? super J , ?>> findNaturalIdAttributes () {
274274 if ( naturalIdAttributes != null ) {
275275 return naturalIdAttributes ;
276276 }
277-
278- if ( getSuperType () != null ) {
277+ else if ( getSuperType () != null ) {
279278 return getSuperType ().findNaturalIdAttributes ();
280279 }
281-
282- return null ;
280+ else {
281+ return null ;
282+ }
283283 }
284284
285285 @ Override
@@ -351,7 +351,7 @@ public void applyNonAggregatedIdAttributes(
351351 AbstractIdentifiableType .this .nonAggregatedIdAttributes = Collections .EMPTY_SET ;
352352 }
353353 else {
354- for ( SingularPersistentAttribute <? super J , ?> idAttribute : idAttributes ) {
354+ for ( var idAttribute : idAttributes ) {
355355 if ( AbstractIdentifiableType .this == idAttribute .getDeclaringType () ) {
356356 addAttribute ( (PersistentAttribute <J , ?>) idAttribute );
357357 }
@@ -389,19 +389,18 @@ public void addAttribute(PersistentAttribute<J, ?> attribute) {
389389 @ Override
390390 public void finishUp () {
391391 managedTypeAccess .finishUp ();
392-
393392 identifierDescriptor = interpretIdDescriptor ();
394393 }
395394 }
396395
397396 private static final Logger log = Logger .getLogger ( AbstractIdentifiableType .class );
398397
399- private PathSource <?> interpretIdDescriptor () {
398+ private SqmPathSource <?> interpretIdDescriptor () {
400399 log .tracef ( "Interpreting domain-model identifier descriptor" );
401400
402- final IdentifiableDomainType <? super J > superType = getSuperType ();
401+ final var superType = getSuperType ();
403402 if ( superType != null ) {
404- final PathSource <?> idDescriptor = superType .getIdentifierDescriptor ();
403+ final var idDescriptor = superType .getIdentifierDescriptor ();
405404 if ( idDescriptor != null ) {
406405 return idDescriptor ;
407406 }
0 commit comments