'The problem that jpa custom query does not take effect

I am designing a 'like' function for a blog system, and I want to judge whether the likes have been added/applied according to the user's login status when querying the content of the homepage.

So I have created a SQL statement to implement the left join to the query to achieve this function. I used the JPA @Query jpql method to customize the query, but the effect is different from what I expected.

When looking at the statement, I found that the jpql statement is indeed executed once, but it is an associated query to another/subsequent jpa query after the initial query was executed, so I think it may be overwritten.

I am using jpa for the first time and they may be thing that I do not fully understand, please feel free to ask any further questions to help me and thank you.

This is my expected sql statement:

SELECT a.*,l.*FROM article a LEFT JOIN user_like_record l ON a.id=l.target_id AND l.target_type=0 AND l.user_id=17

This is my current code:

    @Query(value = "SELECT\n" +
        "\tarticle\n" +
        "FROM\n" +
        "\tArticle article LEFT JOIN article.userLikeRecord ON article.userLikeRecord.targetType = 0\n" +
        "\t AND article.userLikeRecord.userId = :userId")
Page<Article> findAllLikeStatusb(Long userId, Pageable pageable);

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", referencedColumnName = "target_id", insertable = false, updatable = false)
private UserLikeRecord userLikeRecord;

This is the sql statement generated by the last query of jpa:

Hibernate: 
select
    user0_.id as id1_9_,
    user0_.create_time as create_t2_9_,
    user0_.delete_time as delete_t3_9_,
    user0_.update_time as update_t4_9_,
    user0_.email as email5_9_,
    user0_.mobile as mobile6_9_,
    user0_.nick_name as nick_nam7_9_,
    user0_.openid as openid8_9_,
    user0_.receive_like_counts as receive_9_9_,
    user0_.unify_uid as unify_u10_9_,
    user0_.user_birthday as user_bi11_9_,
    user0_.wx_profile as wx_prof12_9_ 
from
    user user0_ 
where
    (
        user0_.delete_time is null
    ) 
    and user0_.openid=?
Hibernate: 
SELECT
    a.*,
    l.* 
FROM
    article a 
LEFT JOIN
    user_like_record l 
        ON a.id = l.target_id 
        AND l.target_type=0 
        AND l.user_id =? 
order by
    a.create_time desc limit ?

The following sql is executed multiple times

Hibernate: 
select
    userlikere0_.target_id as target_i1_10_0_,
    userlikere0_.create_time as create_t2_10_0_,
    userlikere0_.delete_time as delete_t3_10_0_,
    userlikere0_.update_time as update_t4_10_0_,
    userlikere0_.id as id5_10_0_,
    userlikere0_.like_status as like_sta6_10_0_,
    userlikere0_.target_type as target_t7_10_0_,
    userlikere0_.user_id as user_id8_10_0_ 
from
    user_like_record userlikere0_ 
where
    userlikere0_.target_id=? 
    and (
        userlikere0_.delete_time is null
    )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source