spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Join condition gets rendered wrongly if column has an alias [DATAJDBC-353]

Open spring-projects-issues opened this issue 6 years ago • 4 comments

Jens Schauder opened DATAJDBC-353 and commented

This test when added to SelectRendererUnitTest fails because the columns in the join condition get rendered with table-alias.column AS column-alias.  

@Test 
public void shouldRenderJoinWithAliases() {
    Table a = SQL.table("a").as("a_alias"); Column aColumn = a.column("a_col").as("a_col_alias"); 
    Table b = SQL.table("b").as("b_alias"); Column bColumn = b.column("a_col").as("a_col_alias");
    
    Select select = Select.builder().select(aColumn, bColumn) // 
        .from(a) // 
        .join(b).on(aColumn).equals(bColumn) // 
        .build();
 
    assertThat(SqlRenderer.toString(select)) // 
        .isEqualTo(
            "SELECT a_alias.a_col AS a_col_alias, b_alias.b_col AS b_col_alias " // 
            + "FROM a AS a_alias " //
            + "JOIN b AS b_alias ON a_alias.a_col = b_alias.b_col " // 
        ); 
}

Referenced from: pull request https://github.com/spring-projects/spring-data-jdbc/pull/246

spring-projects-issues avatar Apr 05 '19 15:04 spring-projects-issues

Hebert Coelho de Oliveira commented

Hello, I want to help with this ticket. I have a question. I do understand that the join has a problem.

 

But, in the "isEqualTo" condition, on the select statement "b_alias.b_col" should not be "a_col"?

 

The "bColmun" attribute was created with "a_col". should not, the correct select statement be: b_alias.a_col AS a_col_alias that is how the alias for the B is created?

 

I will start looking at the join problem with alias for now.

 

 

spring-projects-issues avatar Aug 26 '20 18:08 spring-projects-issues

Hebert Coelho de Oliveira commented

I also noticed in the join condition. Would not, in the expected query, the correct be "b_alias.a_col"?

spring-projects-issues avatar Aug 26 '20 18:08 spring-projects-issues

Hebert Coelho de Oliveira commented

I created this PR: https://github.com/spring-projects/spring-data-jdbc/pull/246

spring-projects-issues avatar Aug 26 '20 20:08 spring-projects-issues

Jens Schauder commented

You are right, the test I included in the issue is not correct. The definition for the bColumn should be Column bColumn = b.column("b_col").as("b_col_alias")

and the join condition should match that.

(and the column definition should be in its own line).

With this definition everything with a b prefix comes from the b table which should make the test easy to understand.

spring-projects-issues avatar Oct 05 '20 12:10 spring-projects-issues

The problem can no longer be reproduced

schauder avatar Jul 11 '24 08:07 schauder