title: How to generate a DTO from a bidirectional table relation in Spring Boot
body: <p>Good morning, nice to greet you.</p>
  <p>I am new to Spring Boot recently, and I am working with a REST API, which is basically a playlist with songs, basically the REST API should have the following structure. A playlist can have many songs:</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;playList&quot;: 1
          },
          {
              &quot;title&quot;: &quot;Hawai&quot;,
              &quot;artist&quot;: &quot;Maluma&quot;,
              &quot;album&quot;: &quot;PAPI JUANCHO&quot;,
              &quot;year&quot;: 2020,
              &quot;playList&quot;: 1
          }
      ]
  }
  </code></pre>
  <p>Currently this is how I have my entities configured</p>
  <p>Entity SONGS</p>
  <pre><code>@Entity
  @Table(name = &quot;SONGS&quot;)
  public class Songs{
  
      @JsonIgnore
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = &quot;ID&quot;)
      private Long id;
  
      @Column(name = &quot;title&quot;)
      private String title;
  
      @Column(name = &quot;artist&quot;)
      private String artist;
  
      @Column(name = &quot;album&quot;)
      private String album;
  
      @Column(name = &quot;year&quot;)
      private int year;
  
      @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>Entity PLAYLIST</p>
  <pre><code>@Entity
  @Table(name = &quot;PLAY_LIST&quot;)
  public class PlayList {
  
      @JsonIgnore
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = &quot;ID&quot;)
      private Long id;
  
     
      @Column(name=&quot;name&quot;)
      private String name;
  
      
      @Column(name=&quot;description&quot;)
      private String description;
  
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = &quot;playList&quot;)
      private List&lt;Songs&gt; songs = new ArrayList&lt;&gt;();
  
  
      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;Songs&gt; getSongs() {
          return this.songs;
      }
  
      public void setSongs(List&lt;Songs&gt; songs) {
          this.songs = songs;
      }
      
  }
  </code></pre>
  <p>Controller</p>
  <pre><code>@RestController
  @RequestMapping(&quot;playlist&quot;)
  public class PlayListController {
  
      @Autowired
      private PlayListService playListService;
  
      //Get playlist by id with songs belongs that playlist
      
      
      @GetMapping(&quot;/{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 PlayList playList) {
           return playListService.savePlayList(playList);
       }
  
  }
  </code></pre>
  <p>My class PlayListServiceImpl</p>
  <pre><code>@Service
  public class PlayListServiceImpl implements PlayListService {
  
      @Autowired
      private PlayListRepository playListRepository;
  
      public Optional &lt;PlayList&gt; getById(Long Id) {
          Optional &lt;PlayList&gt; playList= playListRepository.findById(Id);
  
          return playList;
      }
  
      @Override
      public PlayList savePlayList(PlayList playList) {
          return playListRepository.save(playList);
      }
      
  }
  </code></pre>
  <p>My Repository</p>
  <pre><code>@Repository
  public interface PlayListRepository extends JpaRepository&lt;PlayList, Long&gt; {
  
      Optional&lt;PlayList&gt; findById(Long Id);
      
  }
  </code></pre>
  <p>However, I have infinite recursion problem when I try to get a playlist with the get method:</p>
  <pre><code>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.example.api.songs.entity.PlayList[\&quot;songs\&quot;]-&gt;org.hibernate.collection.internal.PersistentBag[0]-&gt;com.example.api.songs.entity.Songs[\&quot;playList\&quot;]-&gt;com.example.api.songs.entity.PlayList[\&quot;songs\&quot;]-&gt;org.hibernate.
  </code></pre>
  <p>This is due to the playList field found in the Songs entity, when trying to get the playlist with the songs, I get an array that becomes recursive and not the id of the playList, which is what I want to get, the reality is that I get something like this:</p>
  <p><a href="https://i.stack.imgur.com/9b7b7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9b7b7.png" alt="enter image description here" /></a></p>
  <p>I can solve by applying a JsonIgnore in the field, however this affects me that when I go to save a playlist, I cannot pass the idPlayList field to each song to make sure that each song is saved with its respective idPlayList</p>
  <p>I think the best thing would be to build a DTO here that helps me to have the relationship of the 2 tables, however, I don't know how I would do it in a way that allows me, when executing a get from the playList, to obtain only the idPlayList, and when saving power pass the idPlayList in each song that is going to be saved with a playlist, or if there is another way in which I could store a playlist with its songs and have them stored directly, since currently I have to assign by BD to each song the idPlayList</p>