Spring OneToMany与复合键的关系与另一个复合键的关系

时间:2017-01-21 19:18:03

标签: spring hibernate jpa orm spring-data-jpa

我希望在2个类之间有一个ManyToOne关系(Chapter& Camera)。章节对象包含许多Camera对象。

问题是我的父类(章节)已经有了一个复合键。

章复合键:

@Embeddable
public class ChapterKey implements Serializable {

@Column(name = "Chapter_ID", nullable = false)
private int chapterID;

@Column(name = "Operation_FK", nullable = false)
private int operationFK;
}

章课:

@Entity
public class Chapter implements Serializable
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  private ChapterKey chapterKey;

  @Column(name="Chapter_Name")
  private String chapterName;

  @Column(name="Chapter_Description")
  private String chapterDescription;

  @Column(name="Chapter_View_Range")
  private int chapterViewRangeInterval;

  @Column(name="Chapter_Video_Length")
  private int chapterVideoLength;
 }

相机键:

 @Embeddable
 public class CameraKey implements Serializable {

  @Column(name = "Camera_ID", nullable = false)
  private int cameraID;

  @Column(name = "Chapter_FK", nullable = false)
  private int chapterFK;

  @Column(name = "Operation_FK", nullable = false)
  private int operationFK;
}

相机类:

@Entity
public class Camera implements Serializable
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  private CameraKey cameraKey;

  @Column(name="Camera_Description")
  private String cameraDescription;

  @Column(name="Camera_Usage")
  private int cameraUsage;

  @Column(name="Video_URL")
  private String videoURL;
}

我的问题是,如何在这两个类之间建立OneToMany关系?

2 个答案:

答案 0 :(得分:0)

这是pk(camera_id)&的ER。 pk(chapter_id,operation_fk)问题

enter image description here

这是章课。

    @Entity
public class Camera implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "camera_id", nullable = false)
    private Integer cameraId;

    @Basic(optional = false)
    @Column(name = "camera_description", nullable = false, length = 12)
    private String cameraDescription;

    @Basic(optional = false)
    @Column(name = "camera_usage", nullable = false)
    private int cameraUsage;

    @Basic(optional = false)
    @Column(name = "video_url", nullable = false)
    private int videoUrl;

    @JoinColumn(name = "chapter_fk", referencedColumnName = "chapter_id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Chapter chapterFk;

    @JoinColumn(name = "operation_fk", referencedColumnName = "operation_fk", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Chapter operationFk;  

}

这是ChapterPK.class,它定义了章节类的主键。它有复合主键。

    @Embeddable
public class ChapterPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "chapter_id", nullable = false)
    private int chapterId;

    @Basic(optional = false)
    @Column(name = "operation_fk", nullable = false)
    private int operationFk;

}

终于camera.class

@Entity
public class Chapter implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ChapterPK chapterPK;
    @Basic(optional = false)

    @Column(name = "chapter_name", nullable = false, length = 12)
    private String chapterName;

    @Basic(optional = false)
    @Column(name = "chapter_description", nullable = false, length = 12)
    private String chapterDescription;

    @Basic(optional = false)
    @Column(name = "chapter_Vvew_range", nullable = false)
    private int chapterVvewrange;

    @Basic(optional = false)
    @Column(name = "chapter_video_length", nullable = false)
    private int chapterVideoLength;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "chapterFk", fetch = FetchType.EAGER)
    private Collection<Camera> cameraCollection;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "operationFk", fetch = FetchType.EAGER)
    private Collection<Camera> cameraCollection1;

}

在这里,我在章节和相机类中使用双向绑定。如果您使用复合主键。您必须管理其他嵌入式主键对象。

答案 1 :(得分:0)

章节pk(Chapter_ID, Operation_FK)和相机侧pk(camera_id,chapter_fk,operation_fk)

的答案

ER for在下面。

enter image description here

Chapter.class是

@Entity
public class Chapter implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ChapterPK chapterPK;
    @Basic(optional = false)
    @Column(name = "chapter_name", nullable = false, length = 12)
    private String chapterName;
    @Basic(optional = false)
    @Column(name = "chapter_description", nullable = false, length = 12)
    private String chapterDescription;
    @Basic(optional = false)
    @Column(name = "chapter_Vvew_range", nullable = false)
    private int chapterVvewrange;
    @Basic(optional = false)
    @Column(name = "chapter_video_length", nullable = false)
    private int chapterVideoLength;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "chapter")
    private Collection<Camera> cameraCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "chapter1")
    private Collection<Camera> cameraCollection1;


}

ChapterPK.class

@Embeddable
public class ChapterPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "chapter_id", nullable = false)
    private int chapterId;
    @Basic(optional = false)
    @Column(name = "operation_fk", nullable = false)
    private int operationFk;



}

Camera.class

@Entity
public class Camera implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CameraPK cameraPK;

    @Basic(optional = false)
    @Column(name = "camera_description", nullable = false, length = 12)
    private String cameraDescription;

    @Basic(optional = false)
    @Column(name = "camera_usage", nullable = false)
    private int cameraUsage;

    @Basic(optional = false)
    @Column(name = "video_url", nullable = false)
    private int videoUrl;

    @JoinColumn(name = "chapter_fk", referencedColumnName = "chapter_id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Chapter chapter;

    @JoinColumn(name = "operation_fk", referencedColumnName = "operation_fk", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Chapter chapter1;



}

CameraPK.class

@Embeddable
public class CameraPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "camera_id", nullable = false)
    private int cameraId;
    @Basic(optional = false)
    @Column(name = "chapter_fk", nullable = false)
    private int chapterFk;
    @Basic(optional = false)
    @Column(name = "operation_fk", nullable = false)
    private int operationFk;



}

在这里,双方都有双向绑定。