Search code examples
javaspring-bootjpaorm

Delete object from an entity with a OneToMany relationship between both using JPA in Spring Boot


Good afternoon, I am working with a REST API in which I have a playlist that has many songs, for which I am using JPA and the benefits that allow me to make the relationships between the two. Now, if I want to delete a song already added to the PlayList, I can't do it, I show you my classes below

Class PlayList

@Entity
@Table(name = "PLAY_LIST")
public class PlayList {

    @JsonIgnore
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    //@JsonView(View.Get.class)
    @Column(name="name")
    private String name;

    //@JsonView(View.Get.class)
    @Column(name="description")
    private String description;

    //@JsonView(View.Get.class)
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "playList")
    private List<Song> songs = new ArrayList<>();

    @Transient
    public void addSong(Song song) {
        song.setPlayList(this);
        songs.add(song);
    }

    @Transient
    public void removeSong(Song song) {
        songs.remove(song);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public List<Song> getSongs() {
        return this.songs;
    }

    public void setSongs(List<Song> songs) {
        this.songs = songs;
    }
    
}

Here I have the methods of adding songs and removing, however, removing is not working for me.

Class Song

Entity
@Table(name = "SONG")
public class Song{

    @JsonIgnore
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    //@JsonView(View.Create.class)
    @Column(name = "title")
    private String title;

    //@JsonView(View.Create.class)
    @Column(name = "artist")
    private String artist;

    //@JsonView(View.Create.class)
    @Column(name = "album")
    private String album;

    //@JsonView(View.Create.class)
    @Column(name = "year")
    private int year;

    /* 

    Fetch: Esta propiedad se utiliza para determinar cómo debe ser cargada la entidad.
    LAZY (perezoso): Indica que la relación solo se cargará cuando la propiedad sea leída por primera vez */
    //@JsonView(View.Get.class)
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PLAY_LIST_ID")
    private PlayList playList;


    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return this.artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return this.album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public int getYear() {
        return this.year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public PlayList getPlayList() {
        return this.playList;
    }

    public void setPlayList(PlayList playList) {
        this.playList = playList;
    }
    
}

My class Controller

@RestController
@RequestMapping("playlist")
public class PlayListController {

   @Autowired
   private PlayListService playListService;

   @Autowired
   private SongRepository songRepository;


   // Get playlist by id with songs belongs that playlist

   @GetMapping("/get/{id}")
   public Optional<PlayList> getPlayListByID(@PathVariable(value = "id") Long id) {

      Optional<PlayList> playList = playListService.getById(id);
      return playList;
   }

   @PostMapping("/create")
   public PlayList createPlayList(@RequestBody PlayListDTO playListDTO) {

      PlayList playList = new PlayList();

      playList.setName(playListDTO.getName());
      playList.setDescription(playListDTO.getDescription());
      playList.setSongs(new ArrayList<>());

      for (int x=0; x<playListDTO.getSongs().size(); x++) {

         Song songs=new Song();
         songs.setTitle(playListDTO.getSongs().get(x).getTitle());
         songs.setArtist(playListDTO.getSongs().get(x).getArtist());
         songs.setAlbum(playListDTO.getSongs().get(x).getAlbum());
         songs.setYear(playListDTO.getSongs().get(x).getYear());
         playList.addSong(songs);

      }
        return playListService.savePlayList(playList);
     }
   @PutMapping("/update/{id}")
   public PlayList updatePlayList(@PathVariable(value = "id") Long id,@RequestBody Song song){

      PlayList playList = playListService.getById(id).get();
      song.setPlayList(playList);
      playList.getSongs().add(song);
      playListService.savePlayList(playList);
      return playList;
   }
   
   @DeleteMapping("/delete/{id}")
   public PlayList deletePlayList(@PathVariable(value = "id") Long id,@RequestBody Song song){
      PlayList playList =playListService.getById(id).get();
      System.out.println(playList.getSongs());
      playList.removeSong(song);
      System.out.println(playList.getSongs());
      return playList;
   }

}

So I am storing the Playlist with its songs, method POST

{
    "name": "Lista 1",
    "description": "Lista de reproduccion 2020 spotify",
    "songs": [
        {
            "title": "Tan Enamorados",
            "artist": "CNCO",
            "album": "Tan Enamorados",
            "year": 2020
        },
        {
            "title": "Hawai",
            "artist": "Maluma",
            "album": "PAPI JUANCHO",
            "year": 2020
        }
    ]
}

Now, to eliminate I am passing the id of the PlayList and the Request of the song (object without the id of the song that is automatically created in BD), however, I cannot eliminate the song from the PlayList, and at the logs level it returns this doing a print in console

Example, I want to delete the following song:

enter image description here

however it is not removed and it returns the same list to me

Am I missing something to be able to delete a song without having to delete the playlist?


Solution

  • It is not a very good idea to remove a song using all the songs list in the PlayList. There is not a join table for @OneToMany association. So we can delete a song much simpler, using SONG table (this is the main reason why a join table for @OneToMany is not convienent).

    You need a song id for that and you need to use CrudRepository.deleteById() method. You can use the full combination (title, artist, album, year) for that, but much simpler to add a song id to JSON.

    Better to use this endpoint URL to delete a song

    /{playListId}/songs/{songId}

    You don't need delete part in the URL, you already use DELETE HTTP method.

    Why your code doesn't work

    1. Incorrect using delete method from the list
    @Transient
    public void removeSong(Song song) {
        songs.remove(song);
    }
    

    songs.remove() can't find song in the list, List.remove() finds a song by a reference. It needs to have an open persistent context and get a song from it to have an ability to find it in the list.

    1. Not using a transaction (opened persistent context) to let Hibernate know that a song was deleted and Hibernate has to update the database.

    So a valid scenario

    start @Transactional
      Spring starts a database transaction
      Spring opens a persistent context
      load PlayList
      load a song from the database (using id or other song attributes)
      delete a song from PlayList songs (or delete a song from PlayList songs using id)
    end @Transactional
        
    Hibernate flushes changes and delete a song in the database
    persistent context is closed
    database transaction is committed