@Entity
@Table(name = "people")
public class People {
@ManyToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH})
@JoinTable(name = "bank_people",
joinColumns = @JoinColumn(name = "people_id"),
inverseJoinColumns = @JoinColumn(name = "bank_id"))
private List<Bank> banks;
}
@Entity
@Table(name = "bank")
public class Bank {
@ManyToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.DETACH, CascadeType.MERGE,
CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "bank_people",
joinColumns = @JoinColumn(name = "bank_id"),
inverseJoinColumns = @JoinColumn(name = "people_id"))
private List<People> peoples;
}
当我使用多对多关系时,它会在 json 中无限循环,所以我使用了 @JsonBackReference 和 @JsonManagedReference 但它返回如下
当我搜索 /api/peoples
[
{
"id": 1,
"name": "John",
"accountNumber": "123456",
"accountType": "SA",
"banks": [
{
"id": 1,
"bankName": "Indian Bank",
"branch": "Attur",
"branchCode": "IDIBATR01"
},
{
"id": 2,
"bankName": "State Bank of India",
"branch": "Salem",
"branchCode": "SBIASLM01"
}
]
},
当搜索 /api/banks 时,它只会返回银行而不是人民
[
{
"id": 1,
"bankName": "Indian Bank",
"branch": "Attur",
"branchCode": "IDIBATR01"
},
{
"id": 2,
"bankName": "State Bank of India",
"branch": "Salem",
"branchCode": "SBIASLM01"
}
]
请帮助解决如何像第一个一样在 /api/banks 中返回人