gpt4 book ai didi

java - 为歌曲表 : Spring boot, postgres 编写模型类

转载 作者:行者123 更新时间:2023-11-29 13:18:51 24 4
gpt4 key购买 nike

这是我的表格设计

**Songs

Movie Id - integer 
[year] - integer
Song name - character
Set Singers(list) - text
Lyrics writer(list) -text
Length - numeric(3,2)**

我正在使用 spring boot 开发休息服务。我试图实现一个 pojo,但我没有得到很好的结果。

这是我开发的模型类

@Entity
public class SongsInfo {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
int year;
@ElementCollection
List<String> singers;
@ElementCollection
List<String> lyricists;
float length;



public SongsInfo() {
}



public int getYear() {
return year;
}

public Long getId() {
return id;
}

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

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

public List<String> getSingers() {
return singers;
}

public void setSingers(List<String> singers) {
this.singers = singers;
}

public List<String> getLyricists() {
return Lyricists;
}

public void setLyricists(List<String> lyricists) {
Lyricists = lyricists;
}

public float getLength() {
return length;
}

public void setLength(float length) {
this.length = length;
}

/* public MoviesInfo getMoviesInfo() {
return moviesInfo;
}

@Access(AccessType.PROPERTY)
@ManyToOne
@JoinColumn(name = "movieId")
public void setMoviesInfo(MoviesInfo moviesInfo) {
this.moviesInfo = moviesInfo;
}*/
}

请帮我写一个模型类,我可以在其中实现所有的 CRUD 操作。

我对表中数据的期望:

MovieId year    Song name        setofsingers        lyricists        length
CA1532 2015 adiga adiga hemachandra, roopa ram,sirivennala 5:46

最佳答案

如果您期望的是一列,则不应使用@ElementCollection,因为它会分别创建一个表。 (songs_info、songs_info_lyricists、songs_info_singers)。

您可以使用这样的模型,您可以在其中保存一个简单的列,然后检索一个列表并删除简单歌手和词作者的获取和设置:

模型:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

@Entity
@Table(name = "songs_info")
public class SongsInfo {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private int year;

@Column
private String singers;

@Column
private String lyricists;

private float length;

public long getId() {
return id;
}

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

public int getYear() {
return year;
}

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

public List<String> getSingersList() {
if(this.singers != null){
return Arrays.asList(this.singers.split(","));
}else
return new ArrayList<String>();
}

public void setSingersList(List<String> singersList) {
this.singers = String.join(",", singersList);
}

public List<String> getLyricistsList() {
if(this.lyricists != null){
return Arrays.asList(this.lyricists.split(","));
}else
return new ArrayList<String>();
}

public void setLyricistsList(List<String> lyricistsList) {
this.lyricists = String.join(",", lyricistsList);
}

public float getLength() {
return length;
}

public void setLength(float length) {
this.length = length;
}
}

repo :

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.CrossOrigin;

import com.atos.travel.models.SongsInfo;

@RepositoryRestResource(collectionResourceRel = "songs", path = "songs")
@CrossOrigin("*")
public interface SongsRepository extends PagingAndSortingRepository<SongsInfo, Long> {

}

那么你的帖子应该是这样的:

{ "length": "5.39", "lyricistsList": ["ram","Hemanth" ], "singersList": [ "hema","roopa" ], "year": 2005} 

关于java - 为歌曲表 : Spring boot, postgres 编写模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com