-1

I have a class Book annotated with @Entity and

@ManyToOne
    private BookStore bookStore;

I also have a class BookStore with this code

@OneToMany(mappedBy = "bookStore",fetch = FetchType.EAGER)
    private List<Book> books;

But I get an error in the BookStore class that states: 'One To Many' attribute value type should not be 'Book'. How do I fix this?

I tried adding mappedBy = "bookStore" and both of the classes are annotated with @Data and @Entity.

1 Answer 1

0

You need to provide @JoinColumn of BookStore entity in Book entity to specify the mapping of a foreign key column in a relationship between two entities like below.

@ManyToOne
@JoinColumn(name = "bookstore_id",referencedColumnName = "id")
private BookStore bookStore;

With the help of @JoinColumnn annotation, Hibernate will generate the foreign key constraint in the database to establish the relationship between Book and BookStore entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.