SpringMVC JPA 关联查询问题-根据文章标签查询文章

在做协会网站时,有一个API,是可以通过标签查询对应的文章。所以我在文章的 DAO , IArticleDAO 里有个函数

@Query("select a from ArticleTag a where a.tag in ?1 and a.article.status in ?2")
Page<Article> findArticleByTags(List<Tag> tags, String[] status, Pageable pageable);

但是到我上层 Services 查询回来的结果返回给 Controller 之后,发现返回的 Page<Article> 里面居然装的是 ArticleTag 对象。。。我懵了。查了好久发现原来是查询语句写错了,应该 select a.article 才对,于是改成

@Query("select a.article from ArticleTag a where a.tag in ?1 and a.article.status in ?2")

但是返回的还是 ArticleTag 。。。

于是我又猜了一下,可能是不能这么查询?试了一下改成以 ArticleTag.article 为查询对象

@Query("select a from ArticleTag.article a where ArticleTag.tag in ?1 and a.status in ?2")

于是出现了如下错误直接导致 Services 没法加载

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: ArticleTag.article is not mapped [select a from ArticleTag.article a where ArticleTag.tag in ?1 and a.status in ?2]

好吧。。。。上网再查一下 JPA 查询的文章,我发现了这篇 JPA JPQL 查询、排序.....(转)(那个“转”字我看得很不顺眼。。),里面的 关联(join) 部分 提到

在默认的查询中, Entity 中的集合属性默认不会被关联,集合属性默认是延迟加载 ( lazy-load ) 。

然后重新写了之后就变成现在这个了

@Query("select at.article from ArticleTag at join at.article where at.tag in ?1 and at.article.status in ?2 order by at.article.createDate")

继续使用 PageRequest 来指定通过文章的某个字段排序也是可以的。删掉 order by ,改在 PageRequest 里使用 article.createDate 即可。因为 JPA 查询实际上会把 PageRequest 里的排序拼接在 JQL 语句里面。实际发出去的 JPA 是跟上面在 Query 注解里的查询语句是一样的。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,117评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,892评论 25 709
  • 原文:https://my.oschina.net/liuyuantao/blog/751438 查询集API 参...
    阳光小镇少爷阅读 9,249评论 0 8
  • 我们经常看到很多侃侃而谈的人,教育起别人来,道理一套一套的,然而他自己的人生却过得一团糟。想想我们成长过程中的父母...
    小贤哥2017阅读 4,971评论 0 7
  • 征鸿近乡日暮,看斜阳铺水。棹帆影、千里沧浪,碧落云帟霞袂,入汀渚、长堤系缆,东城紫岭遥相峙。望胡槐坡畔,随风蝶儿相...
    刘小地阅读 3,523评论 25 69