5

I have a class called News. Any news can have many Photo. These are my table

   news                   news_photo             photo
--------------      ----------------------     ------------
| id | title |      | id_news | id_photo |     | id | url |
--------------      ----------------------     ------------
  |                      |         |              |
  |----------------------|         |--------------|

This is my class News

@Entity    
@Table(name = "news")
public class News extends ElementoPersistente implements Serializable{  
    ...
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinTable(name = "news_photo",
               joinColumns = { @JoinColumn(name = "id_news")},
               inverseJoinColumns = { @JoinColumn(name = "id_photo") }) 
    private List<photo> photos;
    ...
}

When I remove an object from List<photo> photos and then I save the object News, the relative record is deleted from the table news_photo but nothing is deleted inside the table photo. How can I delete both records inside news_photo and photo?

Thank you

MDP
  • 4,177
  • 21
  • 63
  • 119
  • I have been digging through some documentation and just noticed your `orphanRemoval = true` on the owner. Do you have the SQL logging turned on when perform the flush operation on the `EntityManager`? – João Rebelo Apr 15 '16 at 13:45
  • Also, what is the JPA provider you're using? – João Rebelo Apr 15 '16 at 14:24
  • Also, there might be another relationship on photo which you are forgetting about and which stops the deletion of that element from the DB. Or even triggers might do it on the DB side. – João Rebelo Apr 15 '16 at 14:42
  • Im using Hibernate and I have nothing else on Photo. I even deleted foreign keys. – MDP Apr 18 '16 at 08:23
  • Do you have the SQL shown with true to see what hibernate is doing in the background? – João Rebelo Apr 18 '16 at 09:04
  • by any chance did you check the database to see what happens with the photo and whether is has left more joins on the `news_photo` table? – João Rebelo Apr 18 '16 at 09:17
  • Have a look at this post. [Orphans remain in database even with orphanRemoval=true on one-to-many relationship (JPA/Hibernate)](http://stackoverflow.com/questions/3304092/orphans-remain-in-database-even-with-orphanremoval-true-on-one-to-many-relations?rq=1) – João Rebelo Apr 18 '16 at 09:23
  • also, you can check my code at https://github.com/joaorebelo-ar/so-36648055 – João Rebelo Apr 18 '16 at 09:45
  • @JoãoRebelo thank for you help. I read the first link, but it doesn't solve my problem ( I even tried to use merge instea of update). I read your code and it's very similar to mine. Are you sure that 'childs' are deleted in their table when a Child is removed from Parent.children list? – MDP Apr 18 '16 at 12:05
  • well without having a look at the code I am unable to help anymore. Have you tried my example in your machine? Somehow it seems the hibernate engine figures there is still a reference to that `photo` in your DB. I see that you're extending `ElementoPersistente`. – João Rebelo Apr 18 '16 at 12:45
  • Are you using the JPA calls (e.g. `merge`, `persist`, etc.) or the Hibernate calls (e.g. `saveOrUpdate`)? For whatever reason this seems to have made a difference in some other scenarios. – Pace May 08 '16 at 13:24

0 Answers0