-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-15725 Criteria API Expression.as adds cast even when the cast type is equal to the expression type #8653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/AbstractCriteriaTest.java
Show resolved
Hide resolved
…e is equal to the expression type
if ( getExpressible() != null ) { | ||
final BasicType<X> basicTypeForJavaType = nodeBuilder().getTypeConfiguration() | ||
.getBasicTypeForJavaType( type ); | ||
if ( basicTypeForJavaType != null | ||
&& getExpressible().getRelationalJavaType().getJavaTypeClass() | ||
== basicTypeForJavaType.getRelationalJavaType().getJavaTypeClass() ) { | ||
return new AsWrapperSqmExpression<>( this, type ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( getExpressible() != null ) { | |
final BasicType<X> basicTypeForJavaType = nodeBuilder().getTypeConfiguration() | |
.getBasicTypeForJavaType( type ); | |
if ( basicTypeForJavaType != null | |
&& getExpressible().getRelationalJavaType().getJavaTypeClass() | |
== basicTypeForJavaType.getRelationalJavaType().getJavaTypeClass() ) { | |
return new AsWrapperSqmExpression<>( this, type ); | |
} | |
} | |
final SqmExpressible<?> expressible = getExpressible(); | |
if ( expressible != null ) { | |
if ( expressible.getExpressibleJavaType().getJavaTypeClass() == type ) { | |
return this; | |
} | |
final BasicType<X> basicTypeForJavaType = nodeBuilder().getTypeConfiguration() | |
.getBasicTypeForJavaType( type ); | |
if ( basicTypeForJavaType == null ) { | |
throw new IllegalArgumentException( "Can't cast expression to unknown type: " + type.getCanonicalName() ); | |
} | |
if ( expressible.getRelationalJavaType().getJavaTypeClass() | |
== basicTypeForJavaType.getRelationalJavaType().getJavaTypeClass() ) { | |
return new AsWrapperSqmExpression<>( basicTypeForJavaType, this ); | |
} | |
} |
public AsWrapperSqmExpression(SqmExpression<?> expression, Class<T> type) { | ||
super( | ||
expression.nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type ), | ||
expression.nodeBuilder() | ||
); | ||
this.expression = expression; | ||
} | ||
|
||
AsWrapperSqmExpression(SqmExpressible<T> type, SqmExpression<?> expression) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public AsWrapperSqmExpression(SqmExpression<?> expression, Class<T> type) { | |
super( | |
expression.nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type ), | |
expression.nodeBuilder() | |
); | |
this.expression = expression; | |
} | |
AsWrapperSqmExpression(SqmExpressible<T> type, SqmExpression<?> expression) { | |
public AsWrapperSqmExpression(SqmExpressible<T> type, SqmExpression<?> expression) { |
expression.appendHqlString( sb ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to decide on a HQL representation for this expression. I propose wrap(.. as ..)
expression.appendHqlString( sb ); | |
} | |
sb.append( "wrap(" ); | |
expression.appendHqlString( sb ); | |
sb.append( " as " ); | |
sb.append( getNodeType().getReturnedClassName() ); | |
sb.append( ")" ); | |
} | |
@Override | |
public <X> SqmExpression<X> as(Class<X> type) { | |
return expression.as( type ); | |
} |
The Javadoc for
I believe that the current implementation: @Override
public <X> SqmExpression<X> as(Class<X> type) {
return nodeBuilder().cast( this, type );
} is just completely wrong. Calling |
The correct implementation is probably something like: @Override @SuppressWarnings("unchecked")
public <X> SqmExpression<X> as(Class<X> type) {
return (SqmExpression<X>) this;
} |
Thanks for pointing that out. I guess we should always return a new |
Side note, EclipseLink always returns the same instance again and just does an unsafe type cast, which kind of also goes against the new part of that specification. I guess the spec should be clarified. Also, the TCK does not have a test for the |
@beikov Yeah, I agree, that's pretty weird, especially since IIRC Mike wrote that Javadoc. OTOH, "returning a new expression object" is pretty explicit to me—I would say no clarification is needed. |
superseded by #8681 |
https://hibernate.atlassian.net/browse/HHH-15725
This is an alternative solution to #5634
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.