org.hibernate.query.criteria.internal.path.SingularAttributeJoin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.query.criteria.internal.path;
import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.CriteriaSubqueryImpl;
import org.hibernate.query.criteria.internal.FromImplementor;
import org.hibernate.query.criteria.internal.PathSource;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
/**
* Models a join based on a singular attribute
*
* @param Represents the parameterized type of the attribute owner
* @param Represents the parameterized type of the attribute
*
* @author Steve Ebersole
*/
public class SingularAttributeJoin extends AbstractJoinImpl {
private final Bindable model;
@SuppressWarnings({ "unchecked" })
public SingularAttributeJoin(
CriteriaBuilderImpl criteriaBuilder,
Class javaType,
PathSource pathSource,
SingularAttribute joinAttribute,
JoinType joinType) {
super( criteriaBuilder, javaType, pathSource, joinAttribute, joinType );
if ( Attribute.PersistentAttributeType.EMBEDDED == joinAttribute.getPersistentAttributeType() ) {
this.model = (Bindable) joinAttribute;
}
else {
if ( javaType != null ) {
this.model = (Bindable) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType );
}
else {
this.model = (Bindable) joinAttribute.getType();
}
}
}
@Override
public SingularAttribute getAttribute() {
return (SingularAttribute) super.getAttribute();
}
@Override
public SingularAttributeJoin correlateTo(CriteriaSubqueryImpl subquery) {
return (SingularAttributeJoin) super.correlateTo( subquery );
}
@Override
protected FromImplementor createCorrelationDelegate() {
return new SingularAttributeJoin(
criteriaBuilder(),
getJavaType(),
getPathSource(),
getAttribute(),
getJoinType()
);
}
@Override
protected boolean canBeJoinSource() {
return true;
}
@Override
@SuppressWarnings("unchecked")
protected ManagedType locateManagedType() {
if ( getModel().getBindableType() == Bindable.BindableType.ENTITY_TYPE ) {
return (ManagedType) getModel();
}
else if ( getModel().getBindableType() == Bindable.BindableType.SINGULAR_ATTRIBUTE ) {
final Type joinedAttributeType = ( (SingularAttribute) getAttribute() ).getType();
if ( !ManagedType.class.isInstance( joinedAttributeType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] as its type is not a ManagedType"
);
}
return (ManagedType) joinedAttributeType;
}
else if ( getModel().getBindableType() == Bindable.BindableType.PLURAL_ATTRIBUTE ) {
final Type elementType = ( (PluralAttribute) getAttribute() ).getElementType();
if ( !ManagedType.class.isInstance( elementType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] (plural) as its element type is not a ManagedType"
);
}
return (ManagedType) elementType;
}
return super.locateManagedType();
}
public Bindable getModel() {
return model;
}
@Override
public SingularAttributeJoin treatAs(Class treatAsType) {
return new TreatedSingularAttributeJoin( this, treatAsType );
}
public static class TreatedSingularAttributeJoin extends SingularAttributeJoin {
private final SingularAttributeJoin original;
private final Class treatAsType;
public TreatedSingularAttributeJoin(SingularAttributeJoin original, Class treatAsType) {
super(
original.criteriaBuilder(),
treatAsType,
original.getPathSource(),
original.getAttribute(),
original.getJoinType()
);
this.original = original;
this.treatAsType = treatAsType;
}
@Override
public String getAlias() {
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
}
@Override
public void prepareAlias(RenderingContext renderingContext) {
if ( getAlias() == null ) {
if ( isCorrelated() ) {
setAlias( getCorrelationParent().getAlias() );
}
else {
setAlias( renderingContext.generateAlias() );
}
}
}
@Override
protected void setAlias(String alias) {
super.setAlias( alias );
original.setAlias( alias );
}
@Override
protected ManagedType locateManagedType() {
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
}
@Override
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
}
}