spring data jpa在@Query中联合查询的问题,急!

有梦想的 2017-12-23 11:24:02

先感谢一下进贴的大佬,客气话不多说,直接说我的问题:
我使用的是jdk1.8+spring boot+spring data开发的一个小项目。
我在使用一个查询的时候,需要联合查询,使用了@Query的注解,但是报错:
{
"cause": null,
"message": "PersistentEntity must not be null!"
}


我的代码是:
@RepositoryRestResource(path = "vote")
public interface ArticleVoteRepository extends JpaRepository<ArticleVote, Integer> {

@Query(nativeQuery = true, value = "select v.id,v.count,v.title, case when exists(select * from vote_join_user where vu.user_id=:user and vu.vote_id=v.id) then 'true' else 'false' end as flag from article_vote v left join vote_join_user vu on v.id = vu.vote_id and vu.user_id=:user")
public List<?> findVoteList(@Param("user") Integer user);
}

我的方法返回值我一直不知道该如何写,我的实体类是ArticleVote,但是我和vote_join_user(VoteJoinUser实体类)这个表进行的联合查询,那我返回值到底该写那个呢? 还是说我的思路不对? 因此我写了List<?> 就报这个错误。

如果我把List<?>改为 List<ArticleVote>。
则就报错:
{
"timestamp": 1513955876179,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessResourceUsageException",
"message": "could not execute query; SQL [select v.id,v.count,v.title, case when exists(select * from vote_join_user where vu.user_id=? and vu.vote_id=v.id) then 'true' else 'false' end as flag from article_vote v left join vote_join_user vu on v.id = vu.vote_id and vu.user_id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query",
"path": "/api/vote/search/findVoteList"
}


因此,求大佬解释一下,我这个联合查询该如何写? sql语句肯定是正确的,在sql里试过的。
...全文
987 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_21320661 2018-01-12
  • 打赏
  • 举报
回复
返回值 改成list<Object[]>
有梦想的 2018-01-11
  • 打赏
  • 举报
回复
咋没人啊111111111111111
有梦想的 2017-12-26
  • 打赏
  • 举报
回复
没有人回答问题吗
有梦想的 2017-12-23
  • 打赏
  • 举报
回复
我主要是我根本无法使用联合查询,哪怕再简单的联合查询,都不可以。是不是springData的联合查询不是我这样写的? 我是php转的java 所以很多问题还不太了解,望解答一下 十分感谢

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧