如何解决使用本机查询在JPA中进行内部联接
我有两个表-
- 播客
- 类别
播客列-podcast_id,podcast_autho,podcast_category_id,podcast_description,podcast_owner,podcast_subcategory_id,podcast_title
类别列-category_id,category_name,category_owner
podcast_category_id
具有Categories
表中的值,podcast_subcategory_id
也具有。
现在,我有一条sql语句,该语句内部连接两个表以在输出中产生类别名称而不是id-
SELECT p.*,c.category_name,c2.category_name AS sub_category_name FROM podcasts p
LEFT JOIN categories c ON p.podcast_category_id = c.category_id
LEFT JOIN categories c2 ON p.podcast_subcategory_id = c2.category_id;
我在JpaRepository
类中使用相同的查询来获取输出-
interface podcastRepository: JpaRepository<podcast,Long> {
@Query(value = "SELECT p.*,c2.category_name AS sub_category_name FROM podcasts p " +
"LEFT JOIN categories c ON p.podcast_category_id = c.category_id " +
"LEFT JOIN categories c2 ON p.podcast_subcategory_id = c2.category_id",nativeQuery = true)
fun getpodcastsByOwner(owner: Long): List<Any>
}
我确实获得了输出,但是它不是JSON格式,这与其他情况下不同,在其他情况下,我不使用Any
(在Java中是Object
),而是使用特定的Entity类。
我得到的-
[
[
8,"krktush",2,"World War 1",1,3,"What Went Wrong","General","Specific"
],[
9,"World War 2","What went right","Specific"
]
]
我期望-
[
{
"id": 16,"author": "krtkush","title": "World War 1","description": "What Went Wrong","category": "2","subCategory": "","owner": 1,"categoryName": General,"subCategoryName": Specific
},{
"id": 22,"title": "World War 2","description": "What Went Right","category": "20","subCategoryName": Specific
}
]
我该如何实现?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。