English 中文(简体)
I wonder why queries are duplicated when using jpa querydsl
原标题:

When loading posts from the posts list, 1 query to find posts that meet the conditions, 1 count query for paging, and 1 query to count comment notifications(Notification function works through HandlerInterceptor) are created.

However, looking at the log, after the 3 queries above, 3 duplicate queries appear the same again, but I don t know why.

Below is the relevant code.

// PostRepositoryImpl

// function called by controller
public Page<BoardResponseForm> selectPostList(String boardCategory, String category, Pageable pageable) {
        List<BoardResponseForm> content = getPostList(boardCategory, category, pageable);
        Long count = getCount(boardCategory, category);
        return new PageImpl<>(content, pageable, count);
}

private Long getCount(String boardCategory, String category) {
        Long count = jpaQueryFactory
                .select(post.count())
                .from(post)
                .where(checkBoardCategory(boardCategory),
                        checkPostCategory(category))
                .fetchOne();

        return count;
}

private List<BoardResponseForm> getPostList(String boardCategory, String category, Pageable pageable) {

        List<BoardResponseForm> content = jpaQueryFactory
                .select(new QBoardResponseForm(
                        post.id,
                        post.viewCount,
                        post.likeCount,
                        post.commentCount,
                        post.title,
                        post.postCategory,
                        account.nickname,
                        post.publishedDateTime))
                .from(post)
                .leftJoin(post.author, account)
                .where(checkBoardCategory(boardCategory),
                        checkPostCategory(category))
                .orderBy(post.id.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetch();

        return content;
}

private BooleanExpression checkBoardCategory(String boardCategory) {
         return post.boardCategory.eq(boardCategory);
}

    private BooleanExpression checkPostCategory(String category) {
        return StringUtils.hasText(category) ? post.postCategory.eq(category) : null;
}

Below is the query log.

After 3 queries go out, the same 3 queries come again.

2023-08-13 11:39:36.636 DEBUG 1592 --- [io-8080-exec-10] org.hibernate.SQL                        : 
    select
        post0_.id as col_0_0_,
        post0_.view_count as col_1_0_,
        post0_.like_count as col_2_0_,
        post0_.comment_count as col_3_0_,
        post0_.title as col_4_0_,
        post0_.post_category as col_5_0_,
        account1_.nickname as col_6_0_,
        post0_.published_date_time as col_7_0_ 
    from
        post post0_ 
    left outer join
        account account1_ 
            on post0_.author_id=account1_.id 
    where
        (
            post0_.title like ? escape  !  
            or post0_.content like ? escape  !  
            or account1_.nickname like ? escape  ! 
        ) 
        and post0_.board_category=? 
        and post0_.post_category=? 
    order by
        post0_.id desc limit ?
2023-08-13 11:39:36.636 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%test%]
2023-08-13 11:39:36.636 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [CLOB] - [%test%]
2023-08-13 11:39:36.636 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [%test%]
2023-08-13 11:39:36.636 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [dimension]
2023-08-13 11:39:36.636 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [notice]
2023-08-13 11:39:36.644 DEBUG 1592 --- [io-8080-exec-10] org.hibernate.SQL                        : 
    select
        count(post0_.id) as col_0_0_ 
    from
        post post0_ cross 
    join
        account account1_ 
    where
        post0_.author_id=account1_.id 
        and (
            post0_.title like ? escape  !  
            or post0_.content like ? escape  !  
            or account1_.nickname like ? escape  ! 
        ) 
        and post0_.board_category=? 
        and post0_.post_category=?
2023-08-13 11:39:36.645 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%test%]
2023-08-13 11:39:36.645 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [CLOB] - [%test%]
2023-08-13 11:39:36.645 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [%test%]
2023-08-13 11:39:36.645 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [dimension]
2023-08-13 11:39:36.645 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [notice]
2023-08-13 11:39:36.650 DEBUG 1592 --- [io-8080-exec-10] org.hibernate.SQL                        : 
    select
        count(notificati0_.id) as col_0_0_ 
    from
        notification notificati0_ 
    where
        notificati0_.account_id=? 
        and notificati0_.checked=?
2023-08-13 11:39:36.651 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2023-08-13 11:39:36.651 TRACE 1592 --- [io-8080-exec-10] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BOOLEAN] - [false]
2023-08-13 11:39:37.011 DEBUG 1592 --- [nio-8080-exec-8] org.hibernate.SQL                        : 
    select
        post0_.id as col_0_0_,
        post0_.view_count as col_1_0_,
        post0_.like_count as col_2_0_,
        post0_.comment_count as col_3_0_,
        post0_.title as col_4_0_,
        post0_.post_category as col_5_0_,
        account1_.nickname as col_6_0_,
        post0_.published_date_time as col_7_0_ 
    from
        post post0_ 
    left outer join
        account account1_ 
            on post0_.author_id=account1_.id 
    where
        (
            post0_.title like ? escape  !  
            or post0_.content like ? escape  !  
            or account1_.nickname like ? escape  ! 
        ) 
        and post0_.board_category=? 
        and post0_.post_category=? 
    order by
        post0_.id desc limit ?
2023-08-13 11:39:37.011 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%test%]
2023-08-13 11:39:37.011 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [CLOB] - [%test%]
2023-08-13 11:39:37.011 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [%test%]
2023-08-13 11:39:37.011 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [dimension]
2023-08-13 11:39:37.011 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [notice]
2023-08-13 11:39:37.015 DEBUG 1592 --- [nio-8080-exec-8] org.hibernate.SQL                        : 
    select
        count(post0_.id) as col_0_0_ 
    from
        post post0_ cross 
    join
        account account1_ 
    where
        post0_.author_id=account1_.id 
        and (
            post0_.title like ? escape  !  
            or post0_.content like ? escape  !  
            or account1_.nickname like ? escape  ! 
        ) 
        and post0_.board_category=? 
        and post0_.post_category=?
2023-08-13 11:39:37.015 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%test%]
2023-08-13 11:39:37.015 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [CLOB] - [%test%]
2023-08-13 11:39:37.015 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [%test%]
2023-08-13 11:39:37.015 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [dimension]
2023-08-13 11:39:37.015 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [notice]
2023-08-13 11:39:37.020 DEBUG 1592 --- [nio-8080-exec-8] org.hibernate.SQL                        : 
    select
        count(notificati0_.id) as col_0_0_ 
    from
        notification notificati0_ 
    where
        notificati0_.account_id=? 
        and notificati0_.checked=?
2023-08-13 11:39:37.020 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2023-08-13 11:39:37.020 TRACE 1592 --- [nio-8080-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BOOLEAN] - [false]
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签