gpt4 book ai didi

java - 删除两个对象之间的关联

转载 作者:行者123 更新时间:2023-12-01 19:35:00 24 4
gpt4 key购买 nike

我在 Spring 中创建应用程序,它存储专辑、音乐家和乐队。专辑可以包含多个乐队和音乐家。我在 AlbumBand/Musician 之间创建了关联。 Jet 我无法删除它。我不想删除对象,只想删除关联。我尝试发送 REST PUT 请求并将 Album 站点上的 musiciansbands 设置为 null,但没有任何反应:

{
"id": 2,
"title": "Lulu",
"bands": null,
"musicians": null,
"duration": {
"hours": 1,
"minutes": 20,
"seconds": 4
},
"releaseDate": "31/10/2011",
"coverPath": "https://upload.wikimedia.org/wikipedia/en/4/40/Lou_Reed_and_Metallica_-_Lulu.jpg",
"spotifyPath": null
}

我创建了以下类和方法来链接 AlbumMusician,但我无法“取消链接”它们:

@RestController
public class AlbumMusicianController {
@Autowired
AlbumRepository albumRepository;
@Autowired
MusicianRepository musicianRepository;

@Transactional
@PostMapping("/musician/{musicianId}/album/{albumId}")
public List<Album> associate(@PathVariable Long musicianId, @PathVariable Long albumId) {
Album album = this.albumRepository.findById(albumId).orElseThrow(() -> new MissingResourceException("Album",
"Album", albumId.toString()));

return this.musicianRepository.findById(musicianId).map((musician) -> { musician.getAlbums().add(album);
return this.musicianRepository.save(musician).getAlbums();
}).orElseThrow(() -> new MissingResourceException("Musician", "Musician", musicianId.toString()));
}
}

非常感谢您的帮助。

以下是必要的来源。

相册类:

@Entity
@Table(name="album")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Album {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="title")
private String title;
@ManyToMany(targetEntity = Band.class, mappedBy = "albums")
@JsonSerialize(using = BandsSerializer.class)
private List<Band> bands;
@ManyToMany(targetEntity = Musician.class, mappedBy = "albums")
@JsonSerialize(using = MusiciansSerializer.class)
private List<Musician> musicians;
@Embedded
@Column(name="duration")
private Duration duration;
@Column(name="releasedate")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET")
private Date releaseDate;
@Column(name="coverpath")
private String coverPath;
@Column(name="spotifypath")
private String spotifyPath;
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 getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public String getCoverPath() {
return coverPath;
}
public void setCoverPath(String coverPath) {
this.coverPath = coverPath;
}
public String getSpotifyPath() {
return spotifyPath;
}
public void setSpotifyPath(String spotifyPath) {
this.spotifyPath = spotifyPath;
}
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;
}
}

音乐家类:

@Entity
@Table(name="musician")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
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="birthdate")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy", timezone="CET")
private Date birthDate;
@Column(name="picturepath")
private String picturePath;
@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 getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getPicturePath() {
return picturePath;
}
public void setPicturePath(String picturePath) {
this.picturePath = picturePath;
}
public List<Album> getAlbums() {
return albums;
}
public void setAlbums(List<Album> albums) {
this.albums = albums;
}
}

乐队类:

@Entity
@Table(name="band")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Band {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="name")
private String name;
@Column(name="picturepath")
private String picturePath;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "album_band",
joinColumns = @JoinColumn(name = "album_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "band_id",
referencedColumnName = "id"))
@JsonSerialize(using = AlbumsSerializer.class)
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 getPicturePath() {
return picturePath;
}
public void setPicturePath(String picturePath) {
this.picturePath = picturePath;
}
public List<Album> getAlbums() {
return albums;
}
public void setAlbums(List<Album> albums) {
this.albums = albums;
}
}

最佳答案

根据您的 JSON 正文,我假设您正在发送针对相册实体的 PUT 请求。我发现有两件事在调整后对我有用。我不确定您是否出于某种原因避免使用它们。

  • 级联规则将相册的更改级联到其关系。
  • 从相册到其关系的连接表的正确实体映射。
    • 不太确定为什么这是一个问题 - Hibernate 在执行时似乎没有抛出任何与此相关的异常,但它似乎没有正确地持久化事物。

这是专辑与音乐家关系的调整后的关系定义。

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="album_musician", joinColumns = @JoinColumn(name = "musician_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "album_id",
referencedColumnName = "id"))
private List<Musician> musicians;

在这种格式中,我能够将专辑中的更改级联到音乐家。您必须对 Band 实体执行类似的操作,才能将操作从专辑级联到 Band。

关于java - 删除两个对象之间的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59229774/

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