I'm trying to create a one-to-one relationship. On the owner side, the bookIsn
(non-primary) is referring to the primary key of the target entity.
Now the issue is if I want to read all reviews (reviewRepository.findAll()
) the following error is thrown if no book is available for the given bookIsn
:
Unable to find com.repository.model.Book with id 1
But the expecting behavior would be that the book entity simply is set to null
if no book could be found. Like it does if I use the reviewId
for joining the column @JoinColumn( name = "review_id", ... )
instead of the review_isn
.
Can somebody explain why it's working with a primary key, but not for a non-primary attribute? What needs to be done to make it work for non-primary attributes as well?
Below the two classes:
Review.java:
@Entity
@Data
@Table(name = "review")
public class Review {
@Id
@Column(name="review_id")
private String reviewId;
@Column(name="book_isn")
private String bookIsn;
@OneToOne
@JoinColumn(
name = "book_isn",
referencedColumn = "book_isn",
insertable = false,
updatable = false)
private Book book;
}
Book.java:
@Entity
@Data
@Table(name = "book")
public class Book {
@Id
@Column(name="book_isn")
private String bookId;
@Column(name="book_name")
private String bookName;
}