I am working on a e-library app with spring boot which allows users to order books (these data is saved in OrderDetails table ). Whenever I am trying to save ordered book for a certain user, it gives me the following exception:
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.springboot.library.domains.OrderDetails.book]
Here is the OrderDetails class:
@Entity
public class OrderDetails{
@EmbeddedId
private OrderDetailsKey key;
@ManyToOne
@MapsId("bookId")
@JoinColumn(name = "book_id")
private Book book;
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "app_user_id")
private AppUser appUser;
private Integer quantity;
public void setBook(Book book) {
this.book = book;
}
// other getters and setters
}
The Book Class
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", insertable = false, updatable = false)
private Long id;
private String isbn;
private String title;
//some other fields
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private Set <OrderDetails> orderDetails;
//getters and setters
**strong text**}
Based on some similar examples I saw, I need to initialize book property in OrderDetails class, but I'm not sure how to do that:
Something like this except that setOrderDetails
method expects a Set<OrderDetails>
and I'm passing a single one and that's where I'm stuck
public void setBook(Book book) {
this.book = book;
book.setOrderDetails(this);
}
I would really appreciate any help :)