恢复多对多关系的元素时遇到问题

时间:2019-08-12 09:47:07

标签: java spring postgresql

我创建了两个类MusicianBand,它们与类Album有很多关系。我还使用PostgreSQL数据库来反映关系数据库中的类。我使用Postman创建了几个MusicianBandAlbum实例。我还在多对多表(album_bandalbum_musician)中的postgres命令行关系中手动创建。但是,当我在BandMusicianAlbum上执行GET请求时,我没有从对应关系中获得元素列表,只有类本身的组件(例如Musician包含字段namesurnamedateOfBirthList<Album> albums(与Album类有多对多关系),我只能检索{{1} },namesurnamedateOfBirth为空)。下面是类和表。

List<Album> albums类:

Album

@Entity @Table(name="album") public class Album { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name="title") private String title; @ManyToMany(targetEntity = Band.class, mappedBy = "albums") private List<Band> bands; @ManyToMany(targetEntity = Musician.class, mappedBy = "albums") private List<Musician> musicians; @Embedded @Column(name="duration") private Duration duration; @Column(name="dateofrelease") @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET") private Date dateOfRelease; @Column(name="coverpath") private String coverPath; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Duration getDuration() { return duration; } public void setDuration(Duration duration) { this.duration = duration; } public Date getDateOfRelease() { return dateOfRelease; } public void setDateOfRelease(Date dateOfRelease) { this.dateOfRelease = dateOfRelease; } public String getCoverPath() { return coverPath; } public void setCoverPath(String coverPath) { this.coverPath = coverPath; } public List<Band> getBands() { return bands; } public void setBands(List<Band> bands) { this.bands = bands; } public List<Musician> getMusicians() { return musicians; } public void setMusicians(List<Musician> musicians) { this.musicians = musicians; } } 类:

Musician

@Entity @Table(name="musician") public class Musician { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name="name") private String name; @Column(name="surname") private String surname; @Column(name="dateofbirth") @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET") private Date dateOfBirth; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "album_musician", joinColumns = @JoinColumn(name = "album_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "musician_id", referencedColumnName = "id")) private List<Album> albums; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } public List<Album> getAlbums() { return albums; } public void setAlbums(List<Album> albums) { this.albums = albums; } } 类:

Band

@Entity @Table(name="band") public class Band { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name="name") private String name; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "album_band", joinColumns = @JoinColumn(name = "album_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "band_id", referencedColumnName = "id")) private List<Album> albums; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Album> getAlbums() { return albums; } public void setAlbums(List<Album> albums) { this.albums = albums; } } 表:

album

Table "public.album" Column | Type | Collation | Nullable | Default ---------------+---------+-----------+----------+--------- id | bigint | | not null | title | text | | | dateofrelease | date | | | coverpath | text | | | hours | integer | | | minutes | integer | | | seconds | integer | | | Indexes: "album_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "album_band" CONSTRAINT "album_band_album_id_fkey" FOREIGN KEY (album_id) REFERENCES album(id) TABLE "album_musician" CONSTRAINT "album_musician_album_id_fkey" FOREIGN KEY (album_id) REFERENCES album(id) 表:

musician

Table "public.musician" Column | Type | Collation | Nullable | Default -------------+--------+-----------+----------+--------- id | bigint | | not null | name | text | | | surname | text | | | dateofbirth | date | | | Indexes: "musician_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "album_musician" CONSTRAINT "album_musician_musician_id_fkey" FOREIGN KEY (musician_id) REFERENCES musician(id) 表:

band

Table "public.band" Column | Type | Collation | Nullable | Default --------+--------+-----------+----------+--------- id | bigint | | not null | name | text | | | Indexes: "band_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "album_band" CONSTRAINT "album_band_band_id_fkey" FOREIGN KEY (band_id) REFERENCES band(id) 表:

album_musician

Table "public.album_musician" Column | Type | Collation | Nullable | Default -------------+--------+-----------+----------+--------- album_id | bigint | | not null | musician_id | bigint | | not null | Indexes: "album_musician_pkey" PRIMARY KEY, btree (album_id, musician_id) Foreign-key constraints: "album_musician_album_id_fkey" FOREIGN KEY (album_id) REFERENCES album(id) "album_musician_musician_id_fkey" FOREIGN KEY (musician_id) REFERENCES musician(id) 表:

album_band

1 个答案:

答案 0 :(得分:0)

我假设您手动创建了联接表 album_band 。让Spring JPA自动为您创建表,从而提高兼容性。