HHH-15338 Remember initial selection node alias in SqmSelection

This commit is contained in:
Christian Beikov 2022-06-15 09:49:37 +02:00
parent 6a227b5ee2
commit cc500d46e8
2 changed files with 33 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import org.hibernate.query.sqm.tree.SqmVisitableNode;
*/
public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T>, SqmVisitableNode {
private final SqmSelectableNode<T> selectableNode;
private final String alias;
public SqmSelection(
SqmSelectableNode<T> selectableNode,
@ -27,6 +28,7 @@ public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T
assert selectableNode != null;
this.selectableNode = selectableNode;
this.alias = selectableNode.getAlias();
}
public SqmSelection(
@ -37,12 +39,13 @@ public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T
assert selectableNode != null;
this.selectableNode = selectableNode;
this.alias = alias;
selectableNode.alias( alias );
}
@Override
public SqmSelection<T> copy(SqmCopyContext context) {
return new SqmSelection<>( selectableNode.copy( context ), nodeBuilder() );
return new SqmSelection<>( selectableNode.copy( context ), alias, nodeBuilder() );
}
@Override
@ -52,8 +55,7 @@ public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T
@Override
public String getAlias() {
// JPA
return selectableNode.getAlias();
return alias;
}
@Override
@ -64,8 +66,8 @@ public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T
@Override
public void appendHqlString(StringBuilder sb) {
selectableNode.appendHqlString( sb );
if ( selectableNode.getAlias() != null ) {
sb.append( " as " ).append( selectableNode.getAlias() );
if ( alias != null ) {
sb.append( " as " ).append( alias );
}
}
}

View File

@ -99,4 +99,30 @@ public class BasicHqlResultTests {
assertThat( result[1] ).isInstanceOf( SimpleComposite.class );
});
}
@Test
public void testSelectingSamePathDifferentAliasOrder1(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final String qry = "select id as id1, id as id2 from SimpleEntity order by id1";
final List<Object[]> list = session.createQuery( qry ).list();
assertThat( list ).hasSize( 1 );
final Object[] result = list.get( 0 );
assertThat( result[0] ).isEqualTo( 1 );
assertThat( result[1] ).isEqualTo( 1 );
});
}
@Test
public void testSelectingSamePathDifferentAliasOrder2(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final String qry = "select id as id1, id as id2 from SimpleEntity order by id2";
final List<Object[]> list = session.createQuery( qry ).list();
assertThat( list ).hasSize( 1 );
final Object[] result = list.get( 0 );
assertThat( result[0] ).isEqualTo( 1 );
assertThat( result[1] ).isEqualTo( 1 );
});
}
}