如何将带有@Param注释的值传递给方法

问题描述 投票:0回答:1

例如,我决定将所有jpql查询移至另一个类

public class QueryUtils {

public static final String  FIND_All_CLUBS= "select c from Club c order by c.club_name";

}

在存储库中,我有这个:

@Query(value = QueryUtils.FIND_All_CLUBS)
Iterable<Club> findAllAndAndOOrderByClub_name();

而且效果很好。但是我坚持那件事:如果我的jpql中有参数,该怎么办?

@Query(value = "select c from Comment c where c.post.post_id = :id order by c.replyTo.comment_id nulls last")
Iterable<Comment> findAllCommentsOfPost(@Param("id") long id);

我试图写这个:

public static String getFIND_ALL_COMMENTS_OF_POST(String id){
    return new StringBuilder("select c from Comment c where c.post.post_id = ").append(id).append(" order by c.replyTo.comment_id nulls last").toString();
}

但是如何将参数从存储库传递到此方法,此代码(我尝试过的无效):

@Query(QueryUtils.getFIND_ALL_COMMENTS_OF_POST(id))
Iterable<Comment> findAllCommentsOfPost(@Param("id") long id);

请帮助我!

spring-data-jpa hql jpql
1个回答
0
投票

@ Param的替代品,尝试这一点

@Query(value = "select c from Comment c where c.post.post_id = ?1 order by c.replyTo.comment_id nulls last")
Iterable<Comment> findAllCommentsOfPost(long id);
© www.soinside.com 2019 - 2024. All rights reserved.