我有一个springboot项目,其中包含了工作过的数据库数据,我有一个数据库模式,以前的开发人员编写了这个模式来Map多对多关系。
import javax.persistence.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
@Entity(name = "Image")
@Table(name = "image")
public class Image extends DateAudit {
private String originName;
private String path3;
@OneToMany(mappedBy = "image", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ImageTag> imageTags = new HashSet<>();
public void setImageTags(Set<ImageTag> imageTags) {
this.imageTags = imageTags;
}
public void addImageTag(ImageTag imageTag) {
this.imageTags.add(imageTag);
}
public void addTag(Tag tag) {
ImageTag imageTag = new ImageTag(this,tag);
this.imageTags.add(imageTag);
}
public void removeTag(Tag tag) {
for (Iterator<ImageTag> iterator = imageTags.iterator();
iterator.hasNext(); ) {
ImageTag imageTag = iterator.next();
if (imageTag.getImage().equals(this) &&
imageTag.getTag().equals(tag)) {
iterator.remove();
imageTag.getTag().getImageTags().remove(imageTag);
imageTag.setImage(null);
imageTag.setTag(null);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
Image image = (Image) o;
return Objects.equals(originName, image.originName);
}
@Override
public int hashCode() {
return Objects.hash(originName);
}
}
标记表
@Entity(name = "Tag")
@Table(name = "tag")
public class Tag {
@Id
@GeneratedValue
private Long id;
@NaturalId
private String tag;
@OneToMany(mappedBy = "tag")
private Set<ImageTag> imageTags = new HashSet<>();
public Tag() {
}
// getters setters
}
imagetag bunch表,创建多对人的关系
@Entity(name = "ImageTag")
@Table(name = "image_tag")
public class ImageTag {
@EmbeddedId
private ImageTagId id;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("imageId")
private Image image;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("tagId")
private Tag tag;
public ImageTag() {
}
/// gettters/setterg
}
复合键
@Embeddable
public class ImageTagId implements Serializable {
@Column(name = "image_id")
private Long imageId;
@Column(name = "tag_id")
private Long tagId;
public ImageTagId() {
}
public ImageTagId(
Long imageId,
Long tagId) {
this.imageId = imageId;
this.tagId = tagId;
}
// Getters setters
}
当前数据库模式请在此处输入图像描述
例如,创建新图像,实现标签,如将值设置为hashmap
public ImageResponse saveImage(ImageSaveRequest request) {
Image image = imageMapper.toImage(request);
Image finalImage = image;
List<TagResponse> tags = request.getTags().stream()
.map(tagResponse -> {
Optional<Tag> fromDB = tagRepo.findAllByTagLike(tagResponse);
Tag tag = fromDB.orElseGet(() -> tagRepo.save(new Tag(tagResponse)));
ImageTag ig = new ImageTag(finalImage, tag);
finalImage.addImageTag(ig);
tag.addImageTag(ig);
return imageMapper.tagToTagResponse(tag);
}).collect(toList());
image = imageRepo.saveAndFlush(finalImage);
return imageMapper.toImageResponse(image, tags);
}
当我试图编辑当前标记时,我删除了以前的标记并尝试创建新的标记
List<TagResponse> tags = listTags.stream()
.map(tagResponse -> {
Optional<Tag> fromDB = tagRepo.findAllByTagLike(tagResponse);
Tag tag = fromDB.orElseGet(() -> tagRepo.save(new Tag(tagResponse)));
ImageTag ig = new ImageTag(finalImage, tag);
tag.addImageTag(ig);
finalImage.addImageTag(ig);
tag.addImageTag(ig);
return imageMapper.tagToTagResponse(tag);
}).collect(toList());
imageRepo.saveAndFlush(finalImage);
我有个例外:
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.monstabet.game.management.service.db.entity.ImageTag with id com.monstabet.game.management.service.db.entity.ImageTagId@17f65; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.monstabet.game.management.service.db.entity.ImageTag with id com.monstabet.game.management.service.db.entity.ImageTagId@17f65
如何从图像表正确编辑图像标签?
暂无答案!
目前还没有任何答案,快来回答吧!