Skip to content
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

Duplicate WHERE Clauses Generated When Using @Where Annotation with Fetch Join in Querydsl #3678

Open
HDPark95 opened this issue Feb 6, 2024 · 0 comments
Labels

Comments

@HDPark95
Copy link

HDPark95 commented Feb 6, 2024

Observed vs. expected behavior

When writing a QueryDSL query as demonstrated in the provided code snippet, it's observed that the WHERE clause related to filtering comments based on the delete_yn = 'N' condition is being duplicated in the generated SQL query. This duplication results in an unnecessary repetition of the same condition, as shown in the Hibernate log output:

@Entity(name = "board")
@AllArgsConstructor
@NoArgsConstructor
public class Board {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String title;

    @Column
    private String content;

    @OneToMany(mappedBy = "board")
    @Where(clause = "delete_yn = 'N'")
    private List<Comment> comment;
}
@Test
@DisplayName("querydsl with where fetch join")
void querydsl_with_where_fetch_join() {
    jpaQueryFactory.selectFrom(board)
            .leftJoin(board.comment, comment).fetchJoin()
            .fetch();
}

The expected behavior would be for the WHERE clause to apply the condition only once, ensuring efficient and accurate query execution. However, the observed behavior shows the condition (c1_0.delete_yn = 'N') being applied twice in the SQL query:

Hibernate: select b1_0.id,c1_0.board_id,c1_0.id,c1_0.content,c1_0.delete_yn,b1_0.content,b1_0.title from board b1_0 left join comment c1_0 on b1_0.id=c1_0.board_id and (c1_0.delete_yn = 'N') and (c1_0.delete_yn = 'N')

This issue highlights a potential inefficiency in the query construction process when using QueryDSL with a fetch join, leading to the duplication of WHERE clause conditions.

Steps to reproduce

https://github.com/HDPark95/querydsl-issue

package my.example.querydslissue.querydsl;

querydsl_with_where_fetch_join

method make this issue

Environment

Querydsl version: 5.0.0:jakarta

Database: h2

JDK: 17

spring boot 3.0.1

Additional details

@HDPark95 HDPark95 added the bug label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant