1

Good afternoon everyone.

I'm starting with Spring JSON and API Rest, I'm doing my first practice, which consists of a REST API that brings a playlist, like the following:

{

      "name":"Lista 1",

    "description": "Lista de canciones de Spotify",

     “songs”:

      [

              {

                   "title": "",

                   "artist": "",

                    "album": "",

                    "year": ""

                },

               {

                    "title": "",

                     "artist": "" ,

                      "album": "",

                      "year": ""

                },         

     … ]

}

For practice I would like to create a single table in BD for this JSON, so I create my Entity (I don't know if I'm correct using POJO):

package com.example.api.songs.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "songs_list")
public class SongsList {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "songs")
    private List<Songs> songs;

    public class Songs {

        public String title;
        public String artist;
        public String album;
        public int year;

    }

}

Now I would like to start my table using JPA and spring.jpa.hibernate.ddl-auto = create, but I have doubts with the songs field, since it is a multi-dimensional array. At first something like this occurs to me, but I don't know if it's the right thing to do:

CREATE TABLE LIST_SONGS (

ID INT PRIMARY KEY NOT NULL,

NAME TEXT NOT NULL,

DESCRIPTION VARCHAR(50) NOT NULL,

SONGS VARCHAR[][][]

);

Or do you recommend creating 2 separate entities?

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Cesar Justo
  • 707
  • 2
  • 11
  • 35

1 Answers1

0
  1. Name property in @Entity not for a table name
  2. Always use upper case for table names
  3. Use Long for id
  4. The table name should be something like SONGS_LISTS, because each record is a "song list". Better to find a better name of course.
  5. Always specify names for the foreign key constraints
@Entity
@Table(name = "SONGS_LISTS")
public class SongsListEntity {

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

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "songsList")
    private List<SongEntity> songs = new ArrayList<>();

}

@Entity
@Table(name = "SONGS")
public class SongEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SONGS_LIST_ID")
    private SongsListEntity songsList;

}

what is @JoinColumn and how it is used in Hibernate

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Thank you very much for your corrections, now at the database level from what I see I must create 2 tables, where in the SONGS_LISTS table there is a foreign one for each SONGS, is that right? – Cesar Justo Feb 13 '21 at 20:08
  • @CesarJusto in the `SONGS`, let Hibernate create tables and you will see the correct DDL. Better to specify a name for a foreign key constraint too. – v.ladynev Feb 13 '21 at 20:11
  • So I don't need to create any ddl, just starting this and configuring spring.jpa.hibernate.ddl-auto = create should create my tables, right? – Cesar Justo Feb 13 '21 at 20:15
  • @CesarJusto Yes. After that you can create DDL using a created one as an example, if you want to. – v.ladynev Feb 13 '21 at 20:16
  • Thanks for your help, I was testing, however I have some problems, which I posted in a new question https://stackoverflow.com/questions/66227521/how-to-generate-a-dto-from-a-bidirectional-table-relation-in-spring-boot – Cesar Justo Feb 16 '21 at 16:02