Lombok.hashCode issue with "java.lang.StackOverflowError: null"
Lombok.hashCode issue with "java.lang.StackOverflowError: null"
我有两个表具有如下一对一关系:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "book_dtail_id")
private BookDetail bookDetail;
}
@Entity
@Table(name = "book_detail")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookDetail {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
@Column(name = "number_of_pages")
private Integer numberOfPages;
@OneToOne(mappedBy = "bookDetail")
private Book book;
}
我使用表格输入数据如下
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookForm {
Book book;
BookDetail bookDetail;
}
控制器看起来像这样:
String handleNewBook(Model model){
Book book = new Book();
BookDetail bookDetail = new BookDetail();
BookForm bookForm = new BookForm(book, bookDetail);
model.addAttribute("bookForm", bookForm);
return "index";
}
String handleSaveBookCreate(BookForm bookForm, Model model){
bookForm.getBook().setBookDetail(bookForm.getBookDetail());
bookForm.getBookDetail().setBook(bookForm.getBook());
bookService.save(bookForm.getBook()));
return "index";
}
最后是我的表格如下:
<form role="form" action="#" th:object="${bookForm}" th:action="@{/book}" method="POST">
<input type="text" th:field="*{book.name}"/>
<input type="text" th:filed="*{bookDetail} == null ? '' : *{bookDetail.numberOfPages}" placeholder="Enter Book Page Numbers"/>
<button type="submit">Submit</button>
</form>
一切似乎都没有问题,但是当我执行"bookService.save(bookForm.getBook()));"时,出现如下错误
java.lang.WhosebugError: null,
at com.zangland.study.jpa.entity.BookDetail.hashCode(BookDetail.java:17) ~[classes/:na]
at com.zangland.study.jpa.entity.Book.hashCode(Book.java:16) ~[classes/:na]
at com.zangland.study.jpa.entity.BookDetail.hashCode(BookDetail.java:17) ~[classes/:na]
at com.zangland.study.jpa.entity.Book.hashCode(Book.java:16) ~[classes/:na]
和上面一样重复大约 100 行....这是否意味着我不能使用 Lombok.hashCode?
保存的书:“32768”,'Spring JPA',“32768”
保存的 BookDetail:“32768”、“1157”
您在 book 和 bookdetails 之间存在循环依赖。您可能需要从 BookDetail
中排除 book
或从 Book
中排除 bookDetail
。
您可以添加 @EqualsAndHashCode(exclude="book")
。有关详细信息,请参阅 EqualsAndHashCode documentation.
lambok 导致循环依赖
原因:toString() 方法
解决方法:
将您的其中一个模型替换为
@Getter
@Setter
public class BookDetail
而不是
@Data
public class BookDetail
我有两个表具有如下一对一关系:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "book_dtail_id")
private BookDetail bookDetail;
}
@Entity
@Table(name = "book_detail")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookDetail {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
@Column(name = "number_of_pages")
private Integer numberOfPages;
@OneToOne(mappedBy = "bookDetail")
private Book book;
}
我使用表格输入数据如下
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookForm {
Book book;
BookDetail bookDetail;
}
控制器看起来像这样:
String handleNewBook(Model model){
Book book = new Book();
BookDetail bookDetail = new BookDetail();
BookForm bookForm = new BookForm(book, bookDetail);
model.addAttribute("bookForm", bookForm);
return "index";
}
String handleSaveBookCreate(BookForm bookForm, Model model){
bookForm.getBook().setBookDetail(bookForm.getBookDetail());
bookForm.getBookDetail().setBook(bookForm.getBook());
bookService.save(bookForm.getBook()));
return "index";
}
最后是我的表格如下:
<form role="form" action="#" th:object="${bookForm}" th:action="@{/book}" method="POST">
<input type="text" th:field="*{book.name}"/>
<input type="text" th:filed="*{bookDetail} == null ? '' : *{bookDetail.numberOfPages}" placeholder="Enter Book Page Numbers"/>
<button type="submit">Submit</button>
</form>
一切似乎都没有问题,但是当我执行"bookService.save(bookForm.getBook()));"时,出现如下错误
java.lang.WhosebugError: null,
at com.zangland.study.jpa.entity.BookDetail.hashCode(BookDetail.java:17) ~[classes/:na]
at com.zangland.study.jpa.entity.Book.hashCode(Book.java:16) ~[classes/:na]
at com.zangland.study.jpa.entity.BookDetail.hashCode(BookDetail.java:17) ~[classes/:na]
at com.zangland.study.jpa.entity.Book.hashCode(Book.java:16) ~[classes/:na]
和上面一样重复大约 100 行....这是否意味着我不能使用 Lombok.hashCode?
保存的书:“32768”,'Spring JPA',“32768” 保存的 BookDetail:“32768”、“1157”
您在 book 和 bookdetails 之间存在循环依赖。您可能需要从 BookDetail
中排除 book
或从 Book
中排除 bookDetail
。
您可以添加 @EqualsAndHashCode(exclude="book")
。有关详细信息,请参阅 EqualsAndHashCode documentation.
lambok 导致循环依赖
原因:toString() 方法
解决方法: 将您的其中一个模型替换为
@Getter
@Setter
public class BookDetail
而不是
@Data
public class BookDetail