harrigaturu
harrigaturu

Reputation: 99

Why can't I delete this entity in JPA?

It's very confusing and I can't find a solution.. I keep finding solutions to minor problems, then new ones occur. I need to be able to remove this entity but whenever i use that method, it just does nothing..

I have 4 entities, here they are: Genre:

public class Genre extends MusicEntity {

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "genre_id")
private List<Artist> artistList;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "genre_id")
private List<Album> albumList;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "genre_id")
private List<Track> trackList;

Artist:

public class Artist extends MusicEntity {

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "artist_id")
private List<Album> artistAlbumList;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "artist_id")
private List<Track> artistTrackList;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="genre_id")
private Genre genre;
private String image;

Album: public class Album extends MusicEntity {

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "album_id")
private List<Track> trackList;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "artist_id")
private Artist artist;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "genre_id")
private Genre genre;
private String image;

Track:

public class Track extends MusicEntity {

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "genre_id")
private Genre genre;
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "album_id")
private Album album;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "artist_id")
private Artist artist;

All 4 extend MusicEntity which just contain ID, EntityTitle and Description. Then they all have AttributeOverrides to alter their ID to track_id or album_id, as well as track_description or album_description on all 4.

I can edit albums and replace the tracks inside. But I can't remove an album. Why? What is stopping JPA from doing this when I use the method "albumRepository.delete(deleteAlbum);" or "jpaRepository.delete(entity)"?

Upvotes: 0

Views: 163

Answers (2)

Christine
Christine

Reputation: 5575

As long as there's an Artist instance referencing an Album instance, you cannot remove the Album instance. You need to remove the Album from the Artist first.

Generally, with CascadeType in the annotation you control what happens with children of a class that you delete or where you remove children. See this tutorial

Upvotes: 1

dxjuv
dxjuv

Reputation: 909

Have you made sure there are no artists associated to that album you're trying to delete? If not, then you should be able to delete. If there are some artists, then you won't be able to do so.

On the other hand, If you delete an artist, all the albums of that artist should be deleted.

Upvotes: 0

Related Questions