feat(music): 支持音乐关联多个歌手
- 修改 Music 和 Singer 实体,使用 @ManyToMany 注解实现多对多关联- 在 MusicServiceImpl 中实现歌手名称字符串的分割和处理 - 删除 singer_music 表,改为在代码中处理关联关系
This commit is contained in:
@@ -91,7 +91,12 @@ public class Music {
|
||||
/**
|
||||
* 歌手列表
|
||||
*/
|
||||
@ManyToMany(mappedBy = "musics")
|
||||
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||
@JoinTable(
|
||||
name = "music_singer",
|
||||
joinColumns = @JoinColumn(name = "music_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "singer_id")
|
||||
)
|
||||
private Set<Singer> singers = new HashSet<>();
|
||||
|
||||
/**
|
||||
|
||||
@@ -77,12 +77,7 @@ public class Singer {
|
||||
/**
|
||||
* 歌手关联的音乐
|
||||
*/
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "singer_music",
|
||||
joinColumns = @JoinColumn(name = "singer_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "music_id")
|
||||
)
|
||||
@ManyToMany(mappedBy = "singers")
|
||||
private Set<Music> musics = new HashSet<>();
|
||||
|
||||
/**
|
||||
|
||||
@@ -329,25 +329,22 @@ public class MusicServiceImpl implements MusicService {
|
||||
music.setSingers(singers);
|
||||
} else if (StringUtils.hasText(musicDTO.getSinger())) {
|
||||
// 如果有歌手名称字符串,则处理歌手名称
|
||||
String singerName = musicDTO.getSinger();
|
||||
// 查找是否已存在该歌手
|
||||
Optional<Singer> existingSinger = singerRepository.findByName(singerName);
|
||||
|
||||
if (existingSinger.isPresent()) {
|
||||
// 如果歌手已存在,直接使用
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
singers.add(existingSinger.get());
|
||||
music.setSingers(singers);
|
||||
} else {
|
||||
// 如果歌手不存在,创建新歌手
|
||||
Singer newSinger = new Singer();
|
||||
newSinger.setName(singerName);
|
||||
Singer savedSinger = singerRepository.save(newSinger);
|
||||
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
singers.add(savedSinger);
|
||||
music.setSingers(singers);
|
||||
String[] singerNames = musicDTO.getSinger().split("[,,、]");
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
for (String singerName : singerNames) {
|
||||
String trimmedName = singerName.trim();
|
||||
if (StringUtils.hasText(trimmedName)) {
|
||||
Optional<Singer> existingSinger = singerRepository.findByName(trimmedName);
|
||||
if (existingSinger.isPresent()) {
|
||||
singers.add(existingSinger.get());
|
||||
} else {
|
||||
Singer newSinger = new Singer();
|
||||
newSinger.setName(trimmedName);
|
||||
singers.add(singerRepository.save(newSinger));
|
||||
}
|
||||
}
|
||||
}
|
||||
music.setSingers(singers);
|
||||
}
|
||||
|
||||
// 保存音乐
|
||||
@@ -428,25 +425,22 @@ public class MusicServiceImpl implements MusicService {
|
||||
music.setSingers(singers);
|
||||
} else if (StringUtils.hasText(musicDTO.getSinger())) {
|
||||
// 如果有歌手名称字符串,则处理歌手名称
|
||||
String singerName = musicDTO.getSinger();
|
||||
// 查找是否已存在该歌手
|
||||
Optional<Singer> existingSinger = singerRepository.findByName(singerName);
|
||||
|
||||
if (existingSinger.isPresent()) {
|
||||
// 如果歌手已存在,直接使用
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
singers.add(existingSinger.get());
|
||||
music.setSingers(singers);
|
||||
} else {
|
||||
// 如果歌手不存在,创建新歌手
|
||||
Singer newSinger = new Singer();
|
||||
newSinger.setName(singerName);
|
||||
Singer savedSinger = singerRepository.save(newSinger);
|
||||
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
singers.add(savedSinger);
|
||||
music.setSingers(singers);
|
||||
String[] singerNames = musicDTO.getSinger().split("[,,、]");
|
||||
Set<Singer> singers = new HashSet<>();
|
||||
for (String singerName : singerNames) {
|
||||
String trimmedName = singerName.trim();
|
||||
if (StringUtils.hasText(trimmedName)) {
|
||||
Optional<Singer> existingSinger = singerRepository.findByName(trimmedName);
|
||||
if (existingSinger.isPresent()) {
|
||||
singers.add(existingSinger.get());
|
||||
} else {
|
||||
Singer newSinger = new Singer();
|
||||
newSinger.setName(trimmedName);
|
||||
singers.add(singerRepository.save(newSinger));
|
||||
}
|
||||
}
|
||||
}
|
||||
music.setSingers(singers);
|
||||
}
|
||||
|
||||
// 保存音乐
|
||||
|
||||
@@ -202,17 +202,6 @@ CREATE TABLE `singer` (
|
||||
INDEX `idx_type_id`(`type_id` ASC) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '歌手表' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for singer_music
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `singer_music`;
|
||||
CREATE TABLE `singer_music` (
|
||||
`singer_id` bigint NOT NULL,
|
||||
`music_id` bigint NOT NULL,
|
||||
PRIMARY KEY (`singer_id`, `music_id`) USING BTREE,
|
||||
INDEX `FKk0fdv208e2ulhcxd9gge63h6j`(`music_id` ASC) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for singer_type
|
||||
-- ----------------------------
|
||||
|
||||
Reference in New Issue
Block a user