HHH-8488 - Fix Join, CollectionJoin, SetJoin, ListJoin, MapJoin CriteriaBuilder#treat SetJoin behaviour
(cherry picked from commit 20f68d43a4
)
This commit is contained in:
parent
abf80cae81
commit
02c86507ba
|
@ -15,6 +15,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import javax.persistence.Tuple;
|
import javax.persistence.Tuple;
|
||||||
import javax.persistence.criteria.CollectionJoin;
|
import javax.persistence.criteria.CollectionJoin;
|
||||||
import javax.persistence.criteria.CompoundSelection;
|
import javax.persistence.criteria.CompoundSelection;
|
||||||
|
@ -1101,31 +1102,31 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <X, T, V extends T> Join<X, V> treat(Join<X, T> join, Class<V> type) {
|
public <X, T, V extends T> Join<X, V> treat(Join<X, T> join, Class<V> type) {
|
||||||
return ( (JoinImplementor) join ).treatAs( type );
|
return treat( join, type, (j, t) -> ((JoinImplementor) j).treatAs( t ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <X, T, E extends T> CollectionJoin<X, E> treat(CollectionJoin<X, T> join, Class<E> type) {
|
public <X, T, E extends T> CollectionJoin<X, E> treat(CollectionJoin<X, T> join, Class<E> type) {
|
||||||
return ( (CollectionJoinImplementor) join ).treatAs( type );
|
return treat( join, type, (j, t) -> ((CollectionJoinImplementor) j).treatAs( t ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <X, T, E extends T> SetJoin<X, E> treat(SetJoin<X, T> join, Class<E> type) {
|
public <X, T, E extends T> SetJoin<X, E> treat(SetJoin<X, T> join, Class<E> type) {
|
||||||
return ( (SetJoinImplementor) join ).treatAs( type );
|
return treat( join, type, (j, t) -> ((SetJoinImplementor) j).treatAs( t ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <X, T, E extends T> ListJoin<X, E> treat(ListJoin<X, T> join, Class<E> type) {
|
public <X, T, E extends T> ListJoin<X, E> treat(ListJoin<X, T> join, Class<E> type) {
|
||||||
return ( (ListJoinImplementor) join ).treatAs( type );
|
return treat( join, type, (j, t) -> ((ListJoinImplementor) join).treatAs( type ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <X, K, T, V extends T> MapJoin<X, K, V> treat(MapJoin<X, K, T> join, Class<V> type) {
|
public <X, K, T, V extends T> MapJoin<X, K, V> treat(MapJoin<X, K, T> join, Class<V> type) {
|
||||||
return ( (MapJoinImplementor) join ).treatAs( type );
|
return treat( join, type, (j, t) -> ((MapJoinImplementor) join).treatAs( type ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1140,7 +1141,6 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
||||||
return ((RootImpl) root).treatAs( type );
|
return ((RootImpl) root).treatAs( type );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// subqueries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// subqueries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1346,4 +1346,14 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable {
|
||||||
public <E, C extends Collection<E>> Predicate isNotMember(Expression<E> eExpression, Expression<C> cExpression) {
|
public <E, C extends Collection<E>> Predicate isNotMember(Expression<E> eExpression, Expression<C> cExpression) {
|
||||||
return isMember(eExpression, cExpression).not();
|
return isMember(eExpression, cExpression).not();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <X, T, V extends T, K extends JoinImplementor> K treat(
|
||||||
|
Join<X, T> join,
|
||||||
|
Class<V> type,
|
||||||
|
BiFunction<Join<X, T>, Class<V>, K> f) {
|
||||||
|
final Set<Join<X, ?>> joins = join.getParent().getJoins();
|
||||||
|
final K treatAs = f.apply( join, type );
|
||||||
|
joins.add(treatAs);
|
||||||
|
return treatAs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,15 @@ import org.hibernate.jpa.criteria.compile.RenderingContext;
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
public interface FromImplementor<Z,X> extends PathImplementor<X>, From<Z,X> {
|
public interface FromImplementor<Z,X> extends PathImplementor<X>, From<Z,X> {
|
||||||
public void prepareAlias(RenderingContext renderingContext);
|
void prepareAlias(RenderingContext renderingContext);
|
||||||
public String renderTableExpression(RenderingContext renderingContext);
|
String renderTableExpression(RenderingContext renderingContext);
|
||||||
|
|
||||||
|
|
||||||
public FromImplementor<Z,X> correlateTo(CriteriaSubqueryImpl subquery);
|
FromImplementor<Z,X> correlateTo(CriteriaSubqueryImpl subquery);
|
||||||
public void prepareCorrelationDelegate(FromImplementor<Z,X> parent);
|
void prepareCorrelationDelegate(FromImplementor<Z,X> parent);
|
||||||
public FromImplementor<Z, X> getCorrelationParent();
|
FromImplementor<Z, X> getCorrelationParent();
|
||||||
|
|
||||||
|
default boolean shouldBeRendered(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -336,6 +336,7 @@ public class QueryStructure<T> implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( Join join : joins ) {
|
for ( Join join : joins ) {
|
||||||
|
if ( ((FromImplementor) join).shouldBeRendered() ) {
|
||||||
((FromImplementor) join).prepareAlias( renderingContext );
|
((FromImplementor) join).prepareAlias( renderingContext );
|
||||||
jpaqlQuery.append( renderJoinType( join.getJoinType() ) )
|
jpaqlQuery.append( renderJoinType( join.getJoinType() ) )
|
||||||
.append( ((FromImplementor) join).renderTableExpression( renderingContext ) );
|
.append( ((FromImplementor) join).renderTableExpression( renderingContext ) );
|
||||||
|
@ -343,6 +344,7 @@ public class QueryStructure<T> implements Serializable {
|
||||||
renderFetches( jpaqlQuery, renderingContext, join.getFetches() );
|
renderFetches( jpaqlQuery, renderingContext, join.getFetches() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String renderJoinType(JoinType joinType) {
|
private String renderJoinType(JoinType joinType) {
|
||||||
switch ( joinType ) {
|
switch ( joinType ) {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import javax.persistence.criteria.Expression;
|
||||||
import javax.persistence.criteria.JoinType;
|
import javax.persistence.criteria.JoinType;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.metamodel.CollectionAttribute;
|
import javax.persistence.metamodel.CollectionAttribute;
|
||||||
|
import javax.persistence.metamodel.ManagedType;
|
||||||
|
|
||||||
import org.hibernate.jpa.criteria.CollectionJoinImplementor;
|
import org.hibernate.jpa.criteria.CollectionJoinImplementor;
|
||||||
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
||||||
|
@ -97,12 +98,35 @@ public class CollectionAttributeJoin<O,E>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return original.getAlias();
|
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepareAlias(RenderingContext renderingContext) {
|
public void prepareAlias(RenderingContext renderingContext) {
|
||||||
// do nothing...
|
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
|
||||||
|
public boolean shouldBeRendered() {
|
||||||
|
if ( getJoins().size() > 0 ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -110,6 +134,11 @@ public class CollectionAttributeJoin<O,E>
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ManagedType<T> locateManagedType() {
|
||||||
|
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPathIdentifier() {
|
public String getPathIdentifier() {
|
||||||
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
|
|
@ -12,6 +12,7 @@ import javax.persistence.criteria.Expression;
|
||||||
import javax.persistence.criteria.JoinType;
|
import javax.persistence.criteria.JoinType;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.metamodel.ListAttribute;
|
import javax.persistence.metamodel.ListAttribute;
|
||||||
|
import javax.persistence.metamodel.ManagedType;
|
||||||
|
|
||||||
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
||||||
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
|
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
|
||||||
|
@ -105,12 +106,35 @@ public class ListAttributeJoin<O,E>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return original.getAlias();
|
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepareAlias(RenderingContext renderingContext) {
|
public void prepareAlias(RenderingContext renderingContext) {
|
||||||
// do nothing...
|
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
|
||||||
|
public boolean shouldBeRendered() {
|
||||||
|
if ( getJoins().size() > 0 ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -118,6 +142,11 @@ public class ListAttributeJoin<O,E>
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ManagedType<T> locateManagedType() {
|
||||||
|
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPathIdentifier() {
|
public String getPathIdentifier() {
|
||||||
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
|
|
@ -12,6 +12,7 @@ import javax.persistence.criteria.Expression;
|
||||||
import javax.persistence.criteria.JoinType;
|
import javax.persistence.criteria.JoinType;
|
||||||
import javax.persistence.criteria.Path;
|
import javax.persistence.criteria.Path;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
|
import javax.persistence.metamodel.ManagedType;
|
||||||
import javax.persistence.metamodel.MapAttribute;
|
import javax.persistence.metamodel.MapAttribute;
|
||||||
|
|
||||||
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
||||||
|
@ -126,12 +127,35 @@ public class MapAttributeJoin<O,K,V>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return original.getAlias();
|
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepareAlias(RenderingContext renderingContext) {
|
public void prepareAlias(RenderingContext renderingContext) {
|
||||||
// do nothing...
|
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
|
||||||
|
public boolean shouldBeRendered() {
|
||||||
|
if ( getJoins().size() > 0 ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -139,6 +163,11 @@ public class MapAttributeJoin<O,K,V>
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ManagedType<T> locateManagedType() {
|
||||||
|
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPathIdentifier() {
|
public String getPathIdentifier() {
|
||||||
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.util.Set;
|
||||||
import javax.persistence.criteria.Expression;
|
import javax.persistence.criteria.Expression;
|
||||||
import javax.persistence.criteria.JoinType;
|
import javax.persistence.criteria.JoinType;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
|
import javax.persistence.metamodel.ManagedType;
|
||||||
import javax.persistence.metamodel.SetAttribute;
|
import javax.persistence.metamodel.SetAttribute;
|
||||||
|
|
||||||
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
||||||
|
@ -102,12 +103,35 @@ public class SetAttributeJoin<O,E>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return original.getAlias();
|
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepareAlias(RenderingContext renderingContext) {
|
public void prepareAlias(RenderingContext renderingContext) {
|
||||||
// do nothing...
|
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
|
||||||
|
public boolean shouldBeRendered() {
|
||||||
|
if ( getJoins().size() > 0 ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -115,6 +139,11 @@ public class SetAttributeJoin<O,E>
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ManagedType<T> locateManagedType() {
|
||||||
|
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPathIdentifier() {
|
public String getPathIdentifier() {
|
||||||
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
|
|
@ -133,12 +133,40 @@ public class SingularAttributeJoin<O,X> extends AbstractJoinImpl<O,X> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return original.getAlias();
|
return isCorrelated() ? getCorrelationParent().getAlias() : super.getAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prepareAlias(RenderingContext renderingContext) {
|
public void prepareAlias(RenderingContext renderingContext) {
|
||||||
// do nothing...
|
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<T> locateManagedType() {
|
||||||
|
return criteriaBuilder().getEntityManagerFactory().getMetamodel().managedType( treatAsType );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean shouldBeRendered() {
|
||||||
|
if ( getJoins().size() > 0 ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue