Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
*/
package org.hibernate.metamodel.model.domain;

import jakarta.persistence.metamodel.Bindable;
import org.hibernate.spi.NavigablePath;

/**
* Any element of the domain model which can be used to create an
* element of a path expression in a query.
*
* @param <J> The type of path element this source creates.
*
* @since 7.0
*
* @author Gavin King
*/
public interface PathSource<J> {
/**
* The name of this thing.
Expand All @@ -17,8 +26,6 @@ public interface PathSource<J> {

/**
* The type of path this source creates.
*
* @apiNote Analogous to {@link Bindable#getBindableJavaType()}.
*/
DomainType<J> getPathType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
import jakarta.persistence.metamodel.Attribute;

import org.hibernate.metamodel.AttributeClassification;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.metamodel.model.domain.PluralPersistentAttribute;
import org.hibernate.query.sqm.tree.domain.SqmDomainType;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.domain.SqmPersistentAttribute;
import org.hibernate.spi.NavigablePath;
import org.hibernate.type.descriptor.java.JavaType;

/**
Expand All @@ -25,7 +30,8 @@
*
* @author Steve Ebersole
*/
public abstract class AbstractAttribute<D,J,B> implements PersistentAttribute<D,J>, Serializable {
public abstract class AbstractAttribute<D,J,B>
implements SqmPersistentAttribute<D,J>, Serializable {
private final ManagedDomainType<D> declaringType;
private final String name;
private final JavaType<J> attributeJtd;
Expand Down Expand Up @@ -96,6 +102,26 @@ public DomainType<?> getValueGraphType() {
return valueType;
}

NavigablePath getParentNavigablePath(SqmPath<?> parent) {
final var parentPathSource = parent.getResolvedModel();
final var parentType = parentPathSource.getPathType();
final NavigablePath parentNavigablePath =
parentPathSource instanceof PluralPersistentAttribute<?, ?, ?>
// for collections, implicitly navigate to the element
? parent.getNavigablePath().append( CollectionPart.Nature.ELEMENT.getName() )
: parent.getNavigablePath();
if ( parentType != declaringType
&& parentType instanceof EntityDomainType<?> entityDomainType
&& entityDomainType.findAttribute( name ) == null ) {
// If the parent path is an entity type which does not contain the
// joined attribute add an implicit treat to the parent's navigable path
return parentNavigablePath.treatAs( declaringType.getTypeName() );
}
else {
return parentNavigablePath;
}
}

@Override
public String toString() {
return declaringType.getTypeName() + '#' + name + '(' + attributeClassification + ')';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import jakarta.persistence.metamodel.Bindable;
import jakarta.persistence.metamodel.IdentifiableType;
import jakarta.persistence.metamodel.SingularAttribute;
Expand All @@ -26,14 +27,15 @@
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.tree.domain.SqmPersistentAttribute;
import org.hibernate.query.sqm.tree.domain.SqmSingularPersistentAttribute;
import org.hibernate.query.sqm.tree.domain.SqmEmbeddableDomainType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.PrimitiveJavaType;

import org.jboss.logging.Logger;

import static java.util.Collections.emptySet;
import static java.util.Collections.emptyList;

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

private SqmSingularPersistentAttribute<J,?> id;
private Set<SingularPersistentAttribute<? super J,?>> nonAggregatedIdAttributes;
private List<SqmSingularPersistentAttribute<? super J,?>> nonAggregatedIdAttributes;
private SqmEmbeddableDomainType<?> idClassType;

private SqmPathSource<?> identifierDescriptor;

private final boolean isVersioned;
private SingularPersistentAttribute<J, ?> versionAttribute;
private List<PersistentAttribute<J,?>> naturalIdAttributes;
private SqmSingularPersistentAttribute<J, ?> versionAttribute;
private List<SqmPersistentAttribute<J,?>> naturalIdAttributes;

public AbstractIdentifiableType(
String typeName,
Expand Down Expand Up @@ -112,13 +114,13 @@ public IdentifiableDomainType<? super J> getSupertype() {

@Override
@SuppressWarnings("unchecked")
public <Y> SingularPersistentAttribute<? super J, Y> getId(Class<Y> javaType) {
public <Y> SqmSingularPersistentAttribute<? super J, Y> getId(Class<Y> javaType) {
ensureNoIdClass();
final var id = findIdAttribute();
if ( id != null ) {
checkType( id, javaType );
}
return (SingularPersistentAttribute<? super J, Y>) id;
return (SqmSingularPersistentAttribute<? super J, Y>) id;
}

private void ensureNoIdClass() {
Expand All @@ -131,7 +133,7 @@ private void ensureNoIdClass() {


@Override
public SingularPersistentAttribute<? super J, ?> findIdAttribute() {
public SqmSingularPersistentAttribute<? super J, ?> findIdAttribute() {
if ( id != null ) {
return id;
}
Expand Down Expand Up @@ -163,13 +165,13 @@ private void checkType(SingularPersistentAttribute<?, ?> attribute, Class<?> jav

@Override
@SuppressWarnings("unchecked")
public <Y> SingularPersistentAttribute<J, Y> getDeclaredId(Class<Y> javaType) {
public <Y> SqmSingularPersistentAttribute<J, Y> getDeclaredId(Class<Y> javaType) {
ensureNoIdClass();
if ( id == null ) {
throw new IllegalArgumentException( "The id attribute is not declared on this type [" + getTypeName() + "]" );
}
checkType( id, javaType );
return (SingularPersistentAttribute<J, Y>) id;
return (SqmSingularPersistentAttribute<J, Y>) id;
}

@Override
Expand Down Expand Up @@ -318,7 +320,7 @@ private void checkDeclaredVersion() {
*
* @return The declared
*/
public SingularAttribute<J, ?> getDeclaredVersion() {
public SqmSingularPersistentAttribute<J, ?> getDeclaredVersion() {
checkDeclaredVersion();
return versionAttribute;
}
Expand Down Expand Up @@ -349,10 +351,12 @@ public void applyNonAggregatedIdAttributes(
}

if ( idAttributes.isEmpty() ) {
nonAggregatedIdAttributes = emptySet();
nonAggregatedIdAttributes = emptyList();
}
else {
nonAggregatedIdAttributes = new ArrayList<>(idAttributes.size());
for ( var idAttribute : idAttributes ) {
nonAggregatedIdAttributes.add( (SqmSingularPersistentAttribute<? super J, ?>) idAttribute );
if ( AbstractIdentifiableType.this == idAttribute.getDeclaringType() ) {
@SuppressWarnings("unchecked")
// Safe, because we know it's declared by this type
Expand All @@ -361,8 +365,6 @@ public void applyNonAggregatedIdAttributes(
addAttribute( declaredAttribute );
}
}

nonAggregatedIdAttributes = idAttributes;
}
AbstractIdentifiableType.this.idClassType = (SqmEmbeddableDomainType<?>) idClassType;
}
Expand All @@ -374,7 +376,8 @@ public void applyIdClassAttributes(Set<SingularPersistentAttribute<? super J, ?>

@Override
public void applyVersionAttribute(SingularPersistentAttribute<J, ?> versionAttribute) {
AbstractIdentifiableType.this.versionAttribute = versionAttribute;
AbstractIdentifiableType.this.versionAttribute =
(SqmSingularPersistentAttribute<J, ?>) versionAttribute;
managedTypeAccess.addAttribute( versionAttribute );
}

Expand All @@ -383,7 +386,7 @@ public void applyNaturalIdAttribute(PersistentAttribute<J, ?> naturalIdAttribute
if ( naturalIdAttributes == null ) {
naturalIdAttributes = new ArrayList<>();
}
naturalIdAttributes.add( naturalIdAttribute );
naturalIdAttributes.add( (SqmPersistentAttribute<J, ?>) naturalIdAttribute );
}

@Override
Expand Down
Loading