【发布时间】: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