1
我有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標註。
請幫忙。
顯示你的'保存()'方法好像正在更新它不建立新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