Skip to content

Commit 4437abc

Browse files
committed
HHH-16972 more fallout from big refactor
1 parent 7bf00aa commit 4437abc

25 files changed

+240
-158
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
*/
55
package org.hibernate.metamodel.model.domain;
66

7-
import jakarta.persistence.metamodel.Bindable;
87
import org.hibernate.spi.NavigablePath;
98

9+
/**
10+
* Any element of the domain model which can be used to create an
11+
* element of a path expression in a query.
12+
*
13+
* @param <J> The type of path element this source creates.
14+
*
15+
* @since 7.0
16+
*
17+
* @author Gavin King
18+
*/
1019
public interface PathSource<J> {
1120
/**
1221
* The name of this thing.
@@ -17,8 +26,6 @@ public interface PathSource<J> {
1726

1827
/**
1928
* The type of path this source creates.
20-
*
21-
* @apiNote Analogous to {@link Bindable#getBindableJavaType()}.
2229
*/
2330
DomainType<J> getPathType();
2431

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
import jakarta.persistence.metamodel.Attribute;
1212

1313
import org.hibernate.metamodel.AttributeClassification;
14+
import org.hibernate.metamodel.mapping.CollectionPart;
1415
import org.hibernate.metamodel.model.domain.DomainType;
16+
import org.hibernate.metamodel.model.domain.EntityDomainType;
1517
import org.hibernate.metamodel.model.domain.ManagedDomainType;
16-
import org.hibernate.metamodel.model.domain.PersistentAttribute;
18+
import org.hibernate.metamodel.model.domain.PluralPersistentAttribute;
1719
import org.hibernate.query.sqm.tree.domain.SqmDomainType;
20+
import org.hibernate.query.sqm.tree.domain.SqmPath;
21+
import org.hibernate.query.sqm.tree.domain.SqmPersistentAttribute;
22+
import org.hibernate.spi.NavigablePath;
1823
import org.hibernate.type.descriptor.java.JavaType;
1924

2025
/**
@@ -25,7 +30,8 @@
2530
*
2631
* @author Steve Ebersole
2732
*/
28-
public abstract class AbstractAttribute<D,J,B> implements PersistentAttribute<D,J>, Serializable {
33+
public abstract class AbstractAttribute<D,J,B>
34+
implements SqmPersistentAttribute<D,J>, Serializable {
2935
private final ManagedDomainType<D> declaringType;
3036
private final String name;
3137
private final JavaType<J> attributeJtd;
@@ -96,6 +102,26 @@ public DomainType<?> getValueGraphType() {
96102
return valueType;
97103
}
98104

105+
NavigablePath getParentNavigablePath(SqmPath<?> parent) {
106+
final var parentPathSource = parent.getResolvedModel();
107+
final var parentType = parentPathSource.getPathType();
108+
final NavigablePath parentNavigablePath =
109+
parentPathSource instanceof PluralPersistentAttribute<?, ?, ?>
110+
// for collections, implicitly navigate to the element
111+
? parent.getNavigablePath().append( CollectionPart.Nature.ELEMENT.getName() )
112+
: parent.getNavigablePath();
113+
if ( parentType != declaringType
114+
&& parentType instanceof EntityDomainType<?> entityDomainType
115+
&& entityDomainType.findAttribute( name ) == null ) {
116+
// If the parent path is an entity type which does not contain the
117+
// joined attribute add an implicit treat to the parent's navigable path
118+
return parentNavigablePath.treatAs( declaringType.getTypeName() );
119+
}
120+
else {
121+
return parentNavigablePath;
122+
}
123+
}
124+
99125
@Override
100126
public String toString() {
101127
return declaringType.getTypeName() + '#' + name + '(' + attributeClassification + ')';

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.Set;
1212
import java.util.function.Consumer;
13+
1314
import jakarta.persistence.metamodel.Bindable;
1415
import jakarta.persistence.metamodel.IdentifiableType;
1516
import jakarta.persistence.metamodel.SingularAttribute;
@@ -26,14 +27,15 @@
2627
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
2728
import org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor;
2829
import org.hibernate.query.sqm.SqmPathSource;
30+
import org.hibernate.query.sqm.tree.domain.SqmPersistentAttribute;
2931
import org.hibernate.query.sqm.tree.domain.SqmSingularPersistentAttribute;
3032
import org.hibernate.query.sqm.tree.domain.SqmEmbeddableDomainType;
3133
import org.hibernate.type.descriptor.java.JavaType;
3234
import org.hibernate.type.descriptor.java.spi.PrimitiveJavaType;
3335

3436
import org.jboss.logging.Logger;
3537

36-
import static java.util.Collections.emptySet;
38+
import static java.util.Collections.emptyList;
3739

3840
/**
3941
* Functionality common to all implementations of {@link IdentifiableType}.
@@ -57,14 +59,14 @@ public abstract class AbstractIdentifiableType<J>
5759
private final boolean hasIdClass;
5860

5961
private SqmSingularPersistentAttribute<J,?> id;
60-
private Set<SingularPersistentAttribute<? super J,?>> nonAggregatedIdAttributes;
62+
private List<SqmSingularPersistentAttribute<? super J,?>> nonAggregatedIdAttributes;
6163
private SqmEmbeddableDomainType<?> idClassType;
6264

6365
private SqmPathSource<?> identifierDescriptor;
6466

6567
private final boolean isVersioned;
66-
private SingularPersistentAttribute<J, ?> versionAttribute;
67-
private List<PersistentAttribute<J,?>> naturalIdAttributes;
68+
private SqmSingularPersistentAttribute<J, ?> versionAttribute;
69+
private List<SqmPersistentAttribute<J,?>> naturalIdAttributes;
6870

6971
public AbstractIdentifiableType(
7072
String typeName,
@@ -112,13 +114,13 @@ public IdentifiableDomainType<? super J> getSupertype() {
112114

113115
@Override
114116
@SuppressWarnings("unchecked")
115-
public <Y> SingularPersistentAttribute<? super J, Y> getId(Class<Y> javaType) {
117+
public <Y> SqmSingularPersistentAttribute<? super J, Y> getId(Class<Y> javaType) {
116118
ensureNoIdClass();
117119
final var id = findIdAttribute();
118120
if ( id != null ) {
119121
checkType( id, javaType );
120122
}
121-
return (SingularPersistentAttribute<? super J, Y>) id;
123+
return (SqmSingularPersistentAttribute<? super J, Y>) id;
122124
}
123125

124126
private void ensureNoIdClass() {
@@ -131,7 +133,7 @@ private void ensureNoIdClass() {
131133

132134

133135
@Override
134-
public SingularPersistentAttribute<? super J, ?> findIdAttribute() {
136+
public SqmSingularPersistentAttribute<? super J, ?> findIdAttribute() {
135137
if ( id != null ) {
136138
return id;
137139
}
@@ -163,13 +165,13 @@ private void checkType(SingularPersistentAttribute<?, ?> attribute, Class<?> jav
163165

164166
@Override
165167
@SuppressWarnings("unchecked")
166-
public <Y> SingularPersistentAttribute<J, Y> getDeclaredId(Class<Y> javaType) {
168+
public <Y> SqmSingularPersistentAttribute<J, Y> getDeclaredId(Class<Y> javaType) {
167169
ensureNoIdClass();
168170
if ( id == null ) {
169171
throw new IllegalArgumentException( "The id attribute is not declared on this type [" + getTypeName() + "]" );
170172
}
171173
checkType( id, javaType );
172-
return (SingularPersistentAttribute<J, Y>) id;
174+
return (SqmSingularPersistentAttribute<J, Y>) id;
173175
}
174176

175177
@Override
@@ -318,7 +320,7 @@ private void checkDeclaredVersion() {
318320
*
319321
* @return The declared
320322
*/
321-
public SingularAttribute<J, ?> getDeclaredVersion() {
323+
public SqmSingularPersistentAttribute<J, ?> getDeclaredVersion() {
322324
checkDeclaredVersion();
323325
return versionAttribute;
324326
}
@@ -349,10 +351,12 @@ public void applyNonAggregatedIdAttributes(
349351
}
350352

351353
if ( idAttributes.isEmpty() ) {
352-
nonAggregatedIdAttributes = emptySet();
354+
nonAggregatedIdAttributes = emptyList();
353355
}
354356
else {
357+
nonAggregatedIdAttributes = new ArrayList<>(idAttributes.size());
355358
for ( var idAttribute : idAttributes ) {
359+
nonAggregatedIdAttributes.add( (SqmSingularPersistentAttribute<? super J, ?>) idAttribute );
356360
if ( AbstractIdentifiableType.this == idAttribute.getDeclaringType() ) {
357361
@SuppressWarnings("unchecked")
358362
// Safe, because we know it's declared by this type
@@ -361,8 +365,6 @@ public void applyNonAggregatedIdAttributes(
361365
addAttribute( declaredAttribute );
362366
}
363367
}
364-
365-
nonAggregatedIdAttributes = idAttributes;
366368
}
367369
AbstractIdentifiableType.this.idClassType = (SqmEmbeddableDomainType<?>) idClassType;
368370
}
@@ -374,7 +376,8 @@ public void applyIdClassAttributes(Set<SingularPersistentAttribute<? super J, ?>
374376

375377
@Override
376378
public void applyVersionAttribute(SingularPersistentAttribute<J, ?> versionAttribute) {
377-
AbstractIdentifiableType.this.versionAttribute = versionAttribute;
379+
AbstractIdentifiableType.this.versionAttribute =
380+
(SqmSingularPersistentAttribute<J, ?>) versionAttribute;
378381
managedTypeAccess.addAttribute( versionAttribute );
379382
}
380383

@@ -383,7 +386,7 @@ public void applyNaturalIdAttribute(PersistentAttribute<J, ?> naturalIdAttribute
383386
if ( naturalIdAttributes == null ) {
384387
naturalIdAttributes = new ArrayList<>();
385388
}
386-
naturalIdAttributes.add( naturalIdAttribute );
389+
naturalIdAttributes.add( (SqmPersistentAttribute<J, ?>) naturalIdAttribute );
387390
}
388391

389392
@Override

0 commit comments

Comments
 (0)