Reputation: 151
I have a problem and I can not find the solution. I have a Book table and an Author table, with many to many relationship. The Book table is in the book schema, and the Author table in the person schema. So I have this:
book.book
people.author
I made the following code:
*** Entity Book ***
@ManyToMany(mappedBy = "book")
private List<Author> author = new ArrayList<>();
and
*** Entity Author ***
@ManyToMany
@JoinTable(schema = "join_tables", name = "book_author", joinColumns = @JoinColumn(name = "fk_author"), inverseJoinColumns = @JoinColumn(name = "fk_book"))
private List<Book> books = new ArrayList<>();
I can register a book with several authors, one author with several books. I can insert an author to a book already registered. Insert a book to an author already registered. But I can not remove the relationship of a book to an author. What I tried to do:
@Repository
public interface AuthorRepository extends JpaRepository<Author, Long> {
@Query("DELETE FROM join_tables.book_author jtba WHERE jtba.fk_author = :fka AND jtba.fk_book = :fkb")
void deleteRelationshipBookAuthor(@Param("fka") Long fka, @Param("fkb") Long fkb);
}
And it always shows the same error:
org.hibernate.hql.internal.ast.QuerySyntaxException: join_tables.book_author is not mapped
But the table exists in the database and has all the data I entered.
Upvotes: 2
Views: 4141
Reputation: 815
A join table is not an entity so you cannot use it in JPQL query(DELETE FROM join_tables.book_author is wrong)
in your relationship, Auther is owning side. so you can remove the book from the books collection and then save the author
author.books.remove(book)
Upvotes: 5