首页 > 解决方案 > 获取一侧时,我可以将 Spring Data Rest/JPA 双向 OneToMany 的多侧嵌入为数组吗

问题描述

我正在使用双向映射探索 Spring Data Rest。我有两张桌子为了清楚起见一张和很多 一张桌子

@OneToMany( mappedBy = "one")
@JsonBackReference
List<Many> many;

许多表

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonManagedReference
@RestResource(exported = false)   
@JoinColumn(name = "one_id")
One one;

正如预期的那样,当我使用 localhost:8080/manies 时,嵌入了 One 引用

"manies": [
{
"rack": "GF",
"shelf": "2",
   "one": {
   "producer": "Escarpment",
   "name": "Kupe"
},
"_links": {
"self": {
"href": "http://localhost:8080/manies/1"
},
"many": {
"href": "http://localhost:8080/manies/1"
}
}
},

我想知道是否有任何方法可以对 localhost:8080/ones 执行相同的操作,即嵌入许多数组 当我尝试时,我只能返回一个链接

"ones": [
{
   "producer": "Escarpment",
    "name": "Kupe",
"_links": {
    "self": {"href": "http://localhost:8080/ones/1"},
    "one": {"href": "http://localhost:8080/ones/1"},
    **"many": {"href": "http://localhost:8080/ones/1/many"}**
    }
},

标签: embedone-to-manyspring-data-restbidirectional

解决方案


推荐阅读