使用本机查询在JPA中进行内部联接

问题描述

我有两个表-

  1. 播客
  2. 类别

播客列-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 (将#修改为@)