Hello,
I am interested is there any way to perform custom DELETE in Hibernate.
Here is situation: I defined many-to-many relation between Album and Artist, so Album can have ONE or MANY Artists and Artist can have ONE or MANY Albums. Here is mapping in Artist: @ManyToMany(targetEntity=Album.class, fetch=FetchType.LAZY) @Cascade({ org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.MERGE }) @JoinTable(name="album_artist", joinColumns={@JoinColumn(name="ArtistID")}, inverseJoinColumns={@JoinColumn(name="AlbumID")}) private Set<Album> albums = new HashSet<Album>();
Now if i modify Artist deleting some albums from set, only specified rows from album_artist table are removed which is ok. If i delete Artist, artist will be deleted and all rows from album_artist table with this ArtistID and that is also what i want. However i would like to delete also Album from album table IF AND ONLY IF that album HAS ONLY THAT ARTIST. Contrary if that Album have other artists also, i don't want to delete album from Album table.
If i specify also org.hibernate.annotations.CascadeType.DELETE, then album will be deleted even if it have other artists!
Is there any way to delete album only if album don't contain other artists?
Best regards.
|