Lombok.hashCode问题" java.lang.StackOverflowError:null"

时间:2016-01-24 06:28:12

标签: hashcode lombok

我有两个表有一对一的关系,如下所示:

@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>
一切似乎没有问题,但是当我&#34; bookService.save(bookForm.getBook()));&#34;执行,我得到如下错误

java.lang.StackOverflowError: 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?

已保存的图书:&#39; 32768&#39;,&#39;春天JPA&#39; 32768&#39; 保存的书籍详细信息:&#39; 32768&#39;,&#39; 1157&#39;

2 个答案:

答案 0 :(得分:39)

书与书籍之间存在循环依赖关系。您可能需要从book排除BookDetail或从bookDetail排除Book

答案 1 :(得分:0)

lambok导致循环依赖

原因:toString()方法

解决方案: 替换您的模型之一

@Getter
@Setter
public class BookDetail

代替

@Data
public class BookDetail