• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate @ManyToMany remove link between tables when record from either table is removed

 
Andrej Kurochenko
Greenhorn
Posts: 3
Linux Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've got two entities Author and Source in @ManyToMany relation. When I remove record from author table I want to remove also links in author_source table, when I remove record from source table I want to remove links from author_source table. How to achieve this functionality? How should I annotate entities and should I remove links in remove() method of DAO by clearing collections?

I've tried something like this

Author entity


Source entity


With this code, whenever I try to remove author, author record is removed and appropriate records from author_source table are also removed. But when I remove source record, links from author_source tables are NOT removed and exception is thrown
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.example.Source#1]


Thanks for replies.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I have a many-to-many relationship, I create a class to represent the relationship table.
You can try creating an AuthorSource class and see what happens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic