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

List<Album> albums类:

@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;
    }
}


Album类:

@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;
    }
}


Musician类:

@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;
    }
}


Band表:

                   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)


album表:

                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)


musician表:

               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)


band表:

             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_musician表:

             Table "public.album_band"
  Column  |  Type  | Collation | Nullable | Default
----------+--------+-----------+----------+---------
 album_id | bigint |           | not null |
 band_id  | bigint |           | not null |
Indexes:
    "album_band_pkey" PRIMARY KEY, btree (album_id, band_id)
Foreign-key constraints:
    "album_band_album_id_fkey" FOREIGN KEY (album_id) REFERENCES album(id)
    "album_band_band_id_fkey" FOREIGN KEY (band_id) REFERENCES band(id)

最佳答案

我假设您是手动创建联接表album_band的,但这不是必需的。让Spring JPA自动为您创建表,从而提高兼容性。

关于java - 恢复多对多关系元素的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57458928/

10-10 17:27