【问题标题】:How to add songs to album entity in springboot?如何在 spring boot 中将歌曲添加到专辑实体?
【发布时间】:2022-11-27 17:58:53
【问题描述】:

请帮助我的逻辑。 是否可以同时在多个实体中保存数据?如果是,那怎么办? 我也必须对歌手实体做同样的事情,即将歌手数据添加到歌曲实体......它需要相同的逻辑还是不同的逻辑。 实体的代码如下:

相册实体


@Entity
@Table(name = "albums")
public class AlbumEntity extends ApplicationPersistenceEntity implements Album {

  @Id
  @Column(name = "album_id")
  @NotNull
  private long albumId;

  @NotEmpty
  @Column(name = "NAME")
  private String albumName;

  @NotEmpty
  @Column(name = "Genre")
  private String genre;

  @Column(name = "album_release_date", columnDefinition = "DATE", nullable = false)
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  private Date albumreleaseDate;

  @ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
  @JoinColumn(name = "singer_id", nullable = false)
  private SingerEntity singer;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "album")
  @Fetch(FetchMode.JOIN)
  private List<SongEntity> songs;

  private static final long serialVersionUID = 1L;

  /**
   * The constructor.
   */
  public AlbumEntity() {

  }

  /**
   * The constructor.
   *
   * @param albumId
   * @param albumName
   * @param genre
   * @param albumreleaseDate
   * @param singer
   * @param songs
   */
  public AlbumEntity(long albumId, String albumName, String genre, Date albumreleaseDate, SingerEntity singer,
      List<SongEntity> songs) {

    super();
    this.albumId = albumId;
    this.albumName = albumName;
    this.genre = genre;
    this.albumreleaseDate = albumreleaseDate;
    this.singer = singer;
    this.songs = songs;
  }

  /**
   * @return albumId
   */
  @Override
  public long getAlbumId() {

    return this.albumId;
  }

  /**
   * @param albumId new value of {@link #getalbumId}.
   */
  @Override
  public void setAlbumId(long albumId) {

    this.albumId = albumId;
  }

  /**
   * @return albumName
   */
  @Override
  public String getAlbumName() {

    return this.albumName;
  }

  /**
   * @param albumName new value of {@link #getalbumName}.
   */
  @Override
  public void setAlbumName(String albumName) {

    this.albumName = albumName;
  }

  /**
   * @return genre
   */
  @Override
  public String getGenre() {

    return this.genre;
  }

  /**
   * @param genre new value of {@link #getgenre}.
   */
  @Override
  public void setGenre(String genre) {

    this.genre = genre;
  }

  /**
   * @return albumreleaseDate
   */
  @Override
  public Date getAlbumreleaseDate() {

    return this.albumreleaseDate;
  }

  /**
   * @param albumreleaseDate new value of {@link #getalbumreleaseDate}.
   */
  @Override
  public void setAlbumreleaseDate(Date albumreleaseDate) {

    this.albumreleaseDate = albumreleaseDate;
  }

  /**
   * @return singer
   */
  public SingerEntity getSinger() {

    return this.singer;
  }

  /**
   * @param singer new value of {@link #getsinger}.
   */
  public void setSinger(SingerEntity singer) {

    this.singer = singer;
  }

  /**
   * @return songs
   */
  public List<SongEntity> getSongs() {

    return this.songs;
  }

  /**
   * @param songs new value of {@link #getsongs}.
   */
  public void setSongs(List<SongEntity> songs) {

    this.songs = songs;
  }

  @Override
  @Transient
  public Long getSingerId() {

    if (this.singer == null) {
      return null;
    }
    return this.singer.getId();
  }

  @Override
  public void setSingerId(Long singerId) {

    if (singerId == null) {
      this.singer = null;
    } else {
      SingerEntity singerEntity = new SingerEntity();
      singerEntity.setId(singerId);
      this.singer = singerEntity;
    }
  }

  @Override
  public String toString() {

    return "AlbumEntity [albumId=" + this.albumId + ", albumName=" + this.albumName + ", genre=" + this.genre
        + ", albumreleaseDate=" + this.albumreleaseDate + ", singer=" + this.singer + ", songs=" + this.songs + "]";
  }

}

歌曲实体

@Entity
@Table(name = "songs")
public class SongEntity extends ApplicationPersistenceEntity implements Song {

  @Id
  @Column(name = "song_id")
  // @GeneratedValue(strategy = GenerationType.TABLE)
  private long songId;

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

  @Column(name = "duration")
  private float duration;

  /**
   * @return duration
   */
  @Override
  public float getDuration() {

    return this.duration;
  }

  /**
   * @param duration new value of {@link #getduration}.
   */
  @Override
  public void setDuration(float duration) {

    this.duration = duration;
  }

  @ManyToOne(cascade = CascadeType.PERSIST, optional = false, fetch = FetchType.LAZY)
  @JoinColumn(name = "singer_id", nullable = false)
  private SingerEntity singer;

  @ManyToOne(cascade = CascadeType.ALL, optional = true, fetch = FetchType.LAZY)
  @JoinColumn(name = "album_id")
  private AlbumEntity album;

  private static final long serialVersionUID = 1L;

  /**
   * The constructor.
   */
  public SongEntity() {

  }

  /**
   * The constructor.
   *
   * @param songId
   * @param title
   * @param content
   * @param singer
   * @param album
   */
  public SongEntity(long songId, String title, SingerEntity singer, AlbumEntity album) {

    super();
    this.songId = songId;
    this.title = title;
    this.singer = singer;
    this.album = album;
  }

  /**
   * @return songId
   */
  @Override
  public long getSongId() {

    return this.songId;
  }

  /**
   * @param songId new value of {@link #getsongId}.
   */
  @Override
  public void setSongId(long songId) {

    this.songId = songId;
  }

  /**
   * @return title
   */
  @Override
  public String getTitle() {

    return this.title;
  }

  /**
   * @param title new value of {@link #gettitle}.
   */
  @Override
  public void setTitle(String title) {

    this.title = title;
  }

  /**
   * @return singer
   */
  public SingerEntity getSinger() {

    return this.singer;
  }

  /**
   * @param singer new value of {@link #getsinger}.
   */
  public void setSinger(SingerEntity singer) {

    this.singer = singer;
  }

  /**
   * @return album
   */
  public AlbumEntity getAlbum() {

    return this.album;
  }

  /**
   * @param album new value of {@link #getalbum}.
   */
  public void setAlbum(AlbumEntity album) {

    this.album = album;
  }

  @Override
  @Transient
  public Long getSingerId() {

    if (this.singer == null) {
      return null;
    }
    return this.singer.getId();
  }

  @Override
  public void setSingerId(Long singerId) {

    if (singerId == null) {
      this.singer = null;
    } else {
      SingerEntity singerEntity = new SingerEntity();
      singerEntity.setId(singerId);
      this.singer = singerEntity;
    }
  }

  @Override
  @Transient
  public Long getAlbumId() {

    if (this.album == null) {
      return null;
    }
    return this.album.getId();
  }

  @Override
  public void setAlbumId(Long albumId) {

    if (albumId == null) {
      this.album = null;
    } else {
      AlbumEntity albumEntity = new AlbumEntity();
      albumEntity.setId(albumId);
      this.album = albumEntity;
    }
  }

  @Override
  public String toString() {

    return "SongEntity [songId=" + this.songId + ", title=" + this.title + ", duration=" + this.duration + ", singer="
        + this.singer + ", album=" + this.album + "]";
  }

}

歌手实体

@Entity
@Table(name = "singers")
public class SingerEntity extends ApplicationPersistenceEntity implements Singer {

  @Id
  @Column(name = "singer_id")
  private long singerId;

  @NotEmpty
  @Column(name = "singer_name")
  private String singerName;

  @NotEmpty
  @Column(name = "Gender")
  private String gender;

  @Column(name = "birth_date", columnDefinition = "DATE", nullable = false)
  @JsonFormat(pattern = "yyyy-MM-dd")
  private Date birthDate;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "singer")
  private List<SongEntity> songs;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "singer")
  private List<AlbumEntity> albums;

  private static final long serialVersionUID = 1L;

  /**
   * The constructor.
   */
  public SingerEntity() {

  }

  /**
   * The constructor.
   *
   * @param singerId
   * @param firstname
   * @param lastname
   * @param gender
   * @param songs
   * @param albums
   */
  public SingerEntity(long singerId, String singerName, String gender, Date birthDate, List<SongEntity> songs,
      List<AlbumEntity> albums) {

    super();
    this.singerId = singerId;
    this.singerName = singerName;
    this.gender = gender;
    this.birthDate = birthDate;
    this.songs = songs;
    this.albums = albums;
  }

  /**
   * @return singerId
   */
  @Override
  public long getSingerId() {

    return this.singerId;
  }

  /**
   * @param singerId new value of {@link #getsingerId}.
   */
  @Override
  public void setSingerId(long singerId) {

    this.singerId = singerId;
  }

  /**
   * @return firstname
   */
  @Override
  public String getSingerName() {

    return this.singerName;
  }

  /**
   * @param singername new value of {@link #getsingername}.
   */
  @Override
  public void setSingerName(String singerName) {

    this.singerName = singerName;
  }

  /**
   * @return gender
   */
  @Override
  public String getGender() {

    return this.gender;
  }

  /**
   * @param gender new value of {@link #getgender}.
   */
  @Override
  public void setGender(String gender) {

    this.gender = gender;
  }

  /**
   * @return birthDate
   */
  @Override
  public Date getBirthDate() {

    return this.birthDate;
  }

  /**
   * @param birthDate new value of {@link #getbirthDate}.
   */
  @Override
  public void setBirthDate(Date birthDate) {

    this.birthDate = birthDate;
  }

  /**
   * @return songs
   */
  public List<SongEntity> getSongs() {

    return this.songs;
  }

  /**
   * @param songs new value of {@link #getsongs}.
   */
  public void setSongs(List<SongEntity> songs) {

    this.songs = songs;
  }

  /**
   * @return albums
   */
  public List<AlbumEntity> getAlbums() {

    return this.albums;
  }

  /**
   * @param albums new value of {@link #getalbums}.
   */
  public void setAlbums(List<AlbumEntity> albums) {

    this.albums = albums;
  }

  @Override
  public String toString() {

    return "SingerEntity [singerId=" + this.singerId + ", singerName=" + this.singerName + ", gender=" + this.gender
        + ", birthDate=" + this.birthDate + ", songs=" + this.songs + ", albums=" + this.albums + "]";
  }

}




I want to add songs to album table.
But I am not able to figure out the logic to do this.
I want that when I send data from post man like


{ “编号”:102, “专辑 ID”:102, "专辑名称": "HS1", “流派”:“流行”, “歌手编号”:202,

"songs":[{"id": 302,
"songId": 302,
"title": "Bad blood",
"singerId": 201,
"duration": 3.76

},

{“编号”:303, “歌曲编号”:303, "title": "快乐", “歌手编号”:202, “持续时间”:3.54

], "专辑发行日期":"2019-05-16" }


Then the data should be saved in album and song table respectively
[enter image description here](https://i.stack.imgur.com/ZOMRY.png)
*
*

【问题讨论】:

    标签: postgresql spring-boot hibernate spring-data-jpa postman


    【解决方案1】:

    我对您的示例数据感到困惑。首先,我认为这是专辑的 POST,因为它包含专辑和 singerID 的所有信息。然后我看到它还包含歌曲的所有信息。这意味着这首歌也将被创作。

    所以这就是我要做的:

    1. 创建相册:

      要求:

      POST /album
      
      {
        "id": 1,
        "genre": "Pop",
        "name": "SomeFancyName",
        "singerId": 12,
        "songs": [
          3,
          5,
          6,
          15//...
        ]
      }
      

      在这里,您将通过 ID 验证所有歌曲的存在,如果歌曲不存在,则返回 404

      回复:

      201 Created
      Headers: Location: "/ablums/{albumId}"
      
      1. 将歌曲添加到专辑

      要求:

      PATCH /album/{albumId}/songs
      
      {
        "songs": [
          7,
          19,
          21,
          13//...
        ]
      }
      

      再次通过 ID 验证所有歌曲是否存在,如果歌曲不存在,则返回 404。

      回复:

      204 No Content
      
      1. 从专辑中删除歌曲 与 2 相同。只需使用 DELETE 方法而不是 PATCH

      Songs 和 Singer 会相应地获得自己的端点,您只能通过请求中的 ID 引用它们。因为,当您在专辑上执行 POST 并添加完整的歌曲数据时,您要么也创建歌曲并且必须考虑如果歌曲已经存在该怎么做,要么发送不必要的数据,因为 id 应该足以识别歌曲。

      基本上,你给一个端点增加了太多的复杂性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2016-03-28
      相关资源
      最近更新 更多