【发布时间】:2021-11-26 20:08:26
【问题描述】:
我正在尝试创建多对多关系。我有一个表“library_branch”,我想加入包含“bookId”、“branchId”和“noOfCopies”的“BookCopies”。我有一个错误ErrorMvcAutoConfiguration$StaticView。我不确定我在哪里导致了这种无限递归,任何帮助将不胜感激。
2021-11-26 14:54:43.544 ERROR 22348 --- [nio-8080-exec-1] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/api/branch] and exception [Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.example.demo.branch.Branch["inventory"]->org.hibernate.collection.internal.PersistentSet[0]->com.example.demo.inventory.Inventory["branch"]->com.example.demo.branch.Branch["inventory"]
InventoryId.java
package com.example.demo.inventory;
...
@Data @AllArgsConstructor
@NoArgsConstructor
@Embeddable
public class InventoryId implements Serializable {
@Column(name = "book_id")
private int bookId;
@Column(name = "branch_id")
private int branchId;
}
Inventory.java
package com.example.demo.inventory;
...
@Entity(name="Inventory")
@Table(name = "tblBookCopies")
public class Inventory {
@EmbeddedId
private InventoryId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("branch_id")
private Branch branch;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("book_id")
private Book book;
@Column(name="no_of_copies")
private int qty;
public Inventory( Branch branch, Book book, int qty) {
this.id = new InventoryId(branch.getId(),book.getId());
this.branch = branch;
this.book = book;
this.qty = qty;
}
...
}
分支.java
package com.example.demo.branch;
...
@Entity(name="Branch")
@Table(name="tblLibraryBranch")
public class Branch {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="address")
private String address;
@OneToMany(mappedBy = "branch", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Inventory> inventory = new HashSet<>();
...
}
【问题讨论】:
标签: java hibernate jpa spring-data-jpa