sysoalt/baba 2022-11-09 07:45 采纳率: 65%
浏览 104
已结题

jpa一对多查询结果有问题

问题遇到的现象和发生背景

jpa一对多查询,结果值总是多一层

用代码块功能插入代码,请勿粘贴截图

这是一表,一个文章对应多条评论

@Entity
@Data
@Table(name = "article")

public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "article_name")
    private String articleName;

    @OneToMany(mappedBy = "article",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JsonIgnoreProperties("article")
    private List<Comment> comments;

}
@Entity
@Data
@Table(name = "comment")
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "article_id",insertable = false,updatable = false)
    private int articleId;

    @Column(name = "content")
    private String content;

    @Column(name = "author")
    private String author;

    @ManyToOne
    @JoinColumn(name = "article_id")
    private Article article;

}
运行结果及报错内容
{
    "id":2,
    "articleId":2,
    "content":"content for bbb",
    "author":"author2",
    "article":{
        "id":2,
        "articleName":"bbb",
        "comments":[
            {
                "id":2,
                "articleId":2,
                "content":"content for bbb",
                "author":"author2"
            },
            {
                "id":3,
                "articleId":2,
                "content":"content also for bbb",
                "author":"author3"
            },
            {
                "id":4,
                "articleId":2,
                "content":"content2 also for bbb",
                "author":"author4"
            }
        ]
    }
}

我想要达到的结果
{"id":2,
        "articleName":"bbb",
        "comments":[
            {
                "id":2,
                "articleId":2,
                "content":"content for bbb",
                "author":"author2"
            },
            {
                "id":3,
                "articleId":2,
                "content":"content also for bbb",
                "author":"author3"
            },
            {
                "id":4,
                "articleId":2,
                "content":"content2 also for bbb",
                "author":"author4"
            }
        ]
}
  • 写回答

4条回答 默认 最新

  • sysoalt/baba 2022-11-14 16:51
    关注

    重写toString方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 心寒丶 全栈领域优质创作者 2022-11-09 08:57
    关注

    你的查询sql怎么写的呢,是查询直接返回的数据有问题,还是查询之后又组装的数据有问题

    评论
  • espalemit_re 2022-11-09 09:05
    关注

    有点没理解 如果你想返回下边的数据的话 数据不是已经返回到前端了吗 可以自己查出来article当中的数据啊。 如果想单纯返回报错内容当中的article 这个得看一下你的代码是怎么写的了

    评论
  • CSDN-Ada助手 CSDN-AI 官方账号 2022-11-09 10:30
    关注
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月22日
  • 已采纳回答 11月14日
  • 修改了问题 11月9日
  • 修改了问题 11月9日
  • 展开全部

悬赏问题

  • ¥15 深海控制器DSE7320MKII和博世ECU间can通讯知识
  • ¥15 Ru的复折射率用于FDTD 200nm到1200nm
  • ¥15 使用Fiddler抓包,textview的乱码如何解决
  • ¥50 trio连接驱动器报错
  • ¥15 有谁懂nhanes的权重计算啊
  • ¥15 欧姆龙PLC 电机控制 限位
  • ¥30 如何处理shell命令接收到的视频流并实时播放出来
  • ¥15 虚心请教C#的代码优化问题
  • ¥15 有偿求做台风过境全过程模拟仿真
  • ¥50 求!AutomationDesk 如何自动导入Variant数据