title: Delete object from an entity with a OneToMany relationship between both using JPA in Spring Boot
body: <p>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</p>
  <p>Class PlayList</p>
  <pre><code>@Entity
  @Table(name = &quot;PLAY_LIST&quot;)
  public class PlayList {
  
      @JsonIgnore
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;ID&quot;)
      private Long id;
  
      //@JsonView(View.Get.class)
      @Column(name=&quot;name&quot;)
      private String name;
  
      //@JsonView(View.Get.class)
      @Column(name=&quot;description&quot;)
      private String description;
  
      //@JsonView(View.Get.class)
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = &quot;playList&quot;)
      private List&lt;Song&gt; songs = new ArrayList&lt;&gt;();
  
      @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&lt;Song&gt; getSongs() {
          return this.songs;
      }
  
      public void setSongs(List&lt;Song&gt; songs) {
          this.songs = songs;
      }
      
  }
  </code></pre>
  <p>Here I have the methods of adding songs and removing, however, removing is not working for me.</p>
  <p>Class Song</p>
  <pre><code>Entity
  @Table(name = &quot;SONG&quot;)
  public class Song{
  
      @JsonIgnore
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;ID&quot;)
      private Long id;
  
      //@JsonView(View.Create.class)
      @Column(name = &quot;title&quot;)
      private String title;
  
      //@JsonView(View.Create.class)
      @Column(name = &quot;artist&quot;)
      private String artist;
  
      //@JsonView(View.Create.class)
      @Column(name = &quot;album&quot;)
      private String album;
  
      //@JsonView(View.Create.class)
      @Column(name = &quot;year&quot;)
      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 = &quot;PLAY_LIST_ID&quot;)
      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;
      }
      
  }
  </code></pre>
  <p>My class Controller</p>
  <pre><code>@RestController
  @RequestMapping(&quot;playlist&quot;)
  public class PlayListController {
  
     @Autowired
     private PlayListService playListService;
  
     @Autowired
     private SongRepository songRepository;
  
  
     // Get playlist by id with songs belongs that playlist
  
     @GetMapping(&quot;/get/{id}&quot;)
     public Optional&lt;PlayList&gt; getPlayListByID(@PathVariable(value = &quot;id&quot;) Long id) {
  
        Optional&lt;PlayList&gt; playList = playListService.getById(id);
        return playList;
     }
  
     @PostMapping(&quot;/create&quot;)
     public PlayList createPlayList(@RequestBody PlayListDTO playListDTO) {
  
        PlayList playList = new PlayList();
  
        playList.setName(playListDTO.getName());
        playList.setDescription(playListDTO.getDescription());
        playList.setSongs(new ArrayList&lt;&gt;());
  
        for (int x=0; x&lt;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(&quot;/update/{id}&quot;)
     public PlayList updatePlayList(@PathVariable(value = &quot;id&quot;) Long id,@RequestBody Song song){
  
        PlayList playList = playListService.getById(id).get();
        song.setPlayList(playList);
        playList.getSongs().add(song);
        playListService.savePlayList(playList);
        return playList;
     }
     
     @DeleteMapping(&quot;/delete/{id}&quot;)
     public PlayList deletePlayList(@PathVariable(value = &quot;id&quot;) 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;
     }
  
  }
  </code></pre>
  <p>So I am storing the Playlist with its songs, method POST</p>
  <pre><code>{
      &quot;name&quot;: &quot;Lista 1&quot;,
      &quot;description&quot;: &quot;Lista de reproduccion 2020 spotify&quot;,
      &quot;songs&quot;: [
          {
              &quot;title&quot;: &quot;Tan Enamorados&quot;,
              &quot;artist&quot;: &quot;CNCO&quot;,
              &quot;album&quot;: &quot;Tan Enamorados&quot;,
              &quot;year&quot;: 2020
          },
          {
              &quot;title&quot;: &quot;Hawai&quot;,
              &quot;artist&quot;: &quot;Maluma&quot;,
              &quot;album&quot;: &quot;PAPI JUANCHO&quot;,
              &quot;year&quot;: 2020
          }
      ]
  }
  </code></pre>
  <p>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</p>
  <p>Example, I want to delete the following song:</p>
  <p><a href="https://i.stack.imgur.com/RAivx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RAivx.png" alt="enter image description here" /></a></p>
  <p>however it is not removed and it returns the same list to me</p>
  <p>Am I missing something to be able to delete a song without having to delete the playlist?</p>