春季休眠ManyToMany
问题描述:
我有2个表,视频,VIDEOCATEGORY和加入表VIDEO2VIDEOCATEGORY。 我正在尝试做很多注释。编译时没有错误或警告。春季休眠ManyToMany
数据库在开始时为空。我试图坚持视频实体(与VideoCategory列表)..在这个时候是所有的声明。新视频被添加到数据库。一些新的类别也被添加。然后,我尝试使用新的独特类别添加新视频...也是正确的。问题是当我尝试添加新的视频与数据库中已经存在的类别。在这次尝试之前,我有2个记录在VIDEO2VIDEOCATEGORY(“cat1”,3)和(“cat2”,3)...尝试后应该有2个新记录 - (“cat1”,4)和(“cat2”,4 )和2个现有的(“cat1”,3)和(“cat2”,3),但那些旧的不在DB中。他们被2条新纪录改写。
如何解决? 我的代码:
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标注。
请帮忙。
答
错误可能是您将双方的父母定义为多对多关系。 一边应该是孩子,像这样:
@ManyToMany(mappedBy = "categories") // in the class VideoCategory
显示你的'保存()'方法好像正在更新它不建立新one.When你认为要添加新的'做Video'确定它实际上是视频的新实例,而不是现有的实例。你的映射是正确的问题是在'save()'或者更可能是你认为是一个新的视频实例,而实际上它不是。 – Shahzeb 2012-03-05 23:54:15
public void save(视频视频){ \t \t getHibernateTemplate()。save(video); \t \t} – falconseye 2012-03-06 00:11:36
我正在创建视频的新实例,没有ID,我试着保存。 – falconseye 2012-03-06 00:14:09