春季休眠ManyToMany



我有两个表,VIDEO、VIDEOCATEGORY和连接表VIDEO2VIDEOOCATEGORY。我正在尝试进行多对多的注释。编译中没有错误或警告。

数据库一开始是空的。我尝试持久化视频实体(带有VideoCategory列表)。。此时一切都是正确的。新视频已添加到数据库中。还添加了一些新类别。然后我尝试添加具有新的独特类别的新视频。。。同样正确。问题是,当我尝试添加DB中已经存在类别的新视频时。在这次尝试之前,我在VIDEO2VIDEOCATEGORY("cat1",3)和("cat2",3。。。在尝试之后,应该有2个新记录-("cat1",4)和("cat2",4。他们被两个新记录改写了。

如何解决?我的代码:


   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

最新更新