gpt4 book ai didi

java - Spring Hibernate 多对多

转载 作者:行者123 更新时间:2023-11-30 04:52:17 25 4
gpt4 key购买 nike

我有 2 个表,VIDEO、VIDEOCATEGORY 和连接表 VIDEO2VIDEOCATEGORY。我正在尝试进行多对多注释。编译时没有任何错误或警告。

数据库一开始是空的。我尝试保留视频实体(带有视频类别列表)..此时一切都是正确的。新视频已添加到数据库中。还添加了一些新类别。然后我尝试添加具有新的独特类别的新视频......也正确。问题是当我尝试添加数据库中已有类别的新视频时。在此尝试之前,我在 VIDEO2VIDEOCATEGORY ("cat1", 3) 和 ("cat2", 3) 中有 2 条记录...尝试后应该有 2 条新记录 - ("cat1", 4) 和 ("cat2", 4 )和 2 个现有的(“cat1”,3)和(“cat2”,3),但那些旧的不在数据库中。它们被两条新记录改写。

如何解决?我的代码:



import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import org.hibernate.annotations.Cascade;


@Entity
public class VideoCategory implements Serializable{

private static final long serialVersionUID = -722840296120424003L;

@Id
@Column(name = "id", unique = true, nullable = false)
private String id;


@ManyToMany(fetch=FetchType.EAGER)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinTable(name = "video2videocategory",
joinColumns = {@JoinColumn(name = "cat_id")}, inverseJoinColumns = @JoinColumn(name = "vid_id"))
private List videos;


public VideoCategory(){
}

public VideoCategory(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id){
this.id = id;
}
public List getVideos() {
return videos;
}
public void setVideos(List videos) {
this.videos = videos;
}

@Override
public String toString() {
return "VideoCategory [id=" + id + ", videos=" + videos + "]";
}
}


import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.JoinColumn;

import org.hibernate.annotations.Cascade;



@Entity
@SequenceGenerator(
name="VID_SEQ_GEN",
sequenceName="VIDSEQ",
allocationSize=1
)
public class Video implements Serializable {

private static final long serialVersionUID = 2284488937952510797L;

@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="VID_SEQ_GEN")
@Column(name = "id", unique = true, nullable = false)
private Long id;

@Column(name = "title", unique = false, nullable = false)
private String title;

@Column(name = "video_path", unique = false, nullable = false)
private String videoPath;

@Column(name = "video_type", unique = false, nullable = false)
@Enumerated(value=EnumType.STRING)
private VideoType videoType;

@Column(name = "creation_date", unique = false, nullable = false)
private Date creationDate;

@ManyToMany
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinTable(name = "video2videocategory",
joinColumns = {@JoinColumn(name = "vid_id")}, inverseJoinColumns = @JoinColumn(name = "cat_id"))
private List categories;



public Video(){
}


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 String getVideoPath() {
return videoPath;
}
public void setVideoPath(String videoPath) {
this.videoPath = videoPath;
}
public VideoType getVideoType() {
return videoType;
}
public void setVideoType(VideoType videoType) {
this.videoType = videoType;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public List getCategories() {
return categories;
}

public void setCategories(List categories) {
this.categories = categories;
}

@Override
public String toString() {
return "Video [id=" + id + ", creationDate="
+ creationDate + ", title=" + title
+ ", videoPath=" + videoPath + ", videoType=" + videoType + "]";
}
}


我尝试过 jpa 和 hibernate 注释。

请帮忙。

最佳答案

错误可能是您将多对多关系定义为双方的父关系。一侧应该是 child ,像这样:

@ManyToMany(mappedBy = "categories") // in the class VideoCategory

关于java - Spring Hibernate 多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575364/

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