I want to Persist an entity Book
which has ManyToOne
relationship with other entity Author
.
@Entity
@Table(name = "BOOK")
public class Book {
private int id;
private Author author;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "AUTHOR_ID")
public Author getAuthor() {
return author;
}
}
So, When I want to save a Book entity, I need to FIND the Author entity using
Author aut = entityManager.find(Author.class, Id);
book.setAuthor(aut);
em.merge(book)
So, for finding of a Author I need to explicitly issue an SELECT query which I don't want to do. Is there any other way that, I can avoid explicit looking for foreign key but can persist the entity with Foreign key value?
Thanks