问题遇到的现象和发生背景
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"
}
]
}