[英]delete row in join table with jpa
我有問題,找不到解決方案。 我有一個Book表和一個Author表,具有多對多關系。 Book表位於book模式中,而Author表位於person模式中。 所以我有這個:
book.book
people.author
我做了以下代碼:
*** Entity Book ***
@ManyToMany(mappedBy = "book")
private List<Author> author = new ArrayList<>();
和
*** 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<>();
我可以注冊幾位作者的書,一位作者可以多本書的書。 我可以在已經注冊的書中插入作者。 將書籍插入已注冊的作者。 但是我無法消除一本書與作者之間的關系。 我試圖做的是:
@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
但是該表存在於數據庫中,並包含我輸入的所有數據。
聯接表不是實體,因此您不能在JPQL查詢中使用它(DELETE FROM join_tables.book_author錯誤)
在您的關系中,Auther是擁有者。 因此您可以從藏書中刪除這本書,然后保存作者
author.books.remove(book)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.