【问题标题】:How pass value with @Param annottaion to a method如何将带有@Param 注释的值传递给方法
【发布时间】:2020-08-04 12:43:32
【问题描述】:

例如,我决定将所有 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 中有参数怎么办,f.e.

@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】:

    @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);
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2022-01-09
      • 2020-04-29
      • 2014-12-10
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多