@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 でも人を返す方法を解決するのを手伝ってください