Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

Lombok.hashCode issue with "java.lang.StackOverflowError: null"

I have two tables has one to one relationship as below:

@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;
}

I used a Form to input data as below

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BookForm {
  Book book;
  BookDetail bookDetail;
}

The controller looks like this:

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";
}

Last is my form as below:

<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>

everything seems no problems, but when I "bookService.save(bookForm.getBook()));" is executed, I got error as below

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]

repeat the same as above about 100 lines.... do this mean that I can't use Lombok.hashCode?

Saved Book: '32768','Spring JPA','32768' Saved BookDetail: '32768','1157'

question from:https://stackoverflow.com/questions/34972895/lombok-hashcode-issue-with-java-lang-stackoverflowerror-null

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a circular dependency between book and bookdetails. You probably need to exclude book from BookDetail or bookDetail from Book.

You can add @EqualsAndHashCode(exclude="book"). For more information, see the EqualsAndHashCode documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...