一个映射到复合主键jpa

时间:2017-11-24 07:03:12

标签: hibernate jpa spring-data-jpa

我有一个用例,两个表有复合主键,一个表与数据库中的另一个表有关系,如何在程序级获得该关系,如何声明bean类

//表Project

的复合主键
    @Embeddable
    @Data
    public class ProjectId implements Serializable {

        private static final long serialVersionUID = 12233365L;

        @Column(name = "PROJECT_CODE")
        private Integer pCode;

        @Column(name = "BOL")
        private String bol;

        @Column(name = "BOLS")
        private String bols;

        @Column(name = "EID")
        private Integer eId;

    }

//项目类

public class Project implements Serializable {

    private static final long serialVersionUID = 748569L;

    @EmbeddedId
    private ProjectId projectId;

    @Column(name = "UPDATED_DATE")
    private Date updatedDate;

    @Column(name = "CREATED_DATE")
    private Date createdDate;

}

// pcode,bol,bols,eId是复合键

//文档表,这里有一些更有趣的项目,文档是" pcode,bol,bols,eId +和#34;

@Embeddable
@Data
public class DocumentId implements Serializable {

    private static final long serialVersionUID = 458933256L;

    @Column(name = "PROJECT_CODE")
    private Integer pCode;

    @Column(name = "BOL")
    private String bol;

    @Column(name = "BOLS")
    private String bols;

    @Column(name = "EID")
    private Integer eId;

    @Column(name = "DID")
    private String dId;
}

//文件表

@Entity
@Table(name = "DOCUMENT")
@Data
public class Documents implements Serializable {

    private static final long serialVersionUID = 14578523695L;

    @EmbeddedId
    private DocumentId documentId;

    @Column(name = "JUR")
    private String jur;

    @Column(name = "DSEQUENCE")
    private Integer dSequence;

    @Column(name = "UPDATED_DATE")
    private Date updatedDate;

    @Column(name = "CREATED_DATE")
    private Date createdDate;

}
  

两个表之间的关系Document是Project的子表,
  我想从项目方面建立一个关系。    从文档到项目也很好

我如何映射表之间的一对一关系??

1 个答案:

答案 0 :(得分:1)

  1. 文档表应该包含Project实体的键列(pCode,bol等..)
  2. 在Document类上添加此成员:

    @OneToOne
    @JoinColumns({@JoinColumn(name = "pCode", referencedColumnName = "PROJECT_CODE"),
                  @JoinColumn(name = "bol", referencedColumnName = "BOL"),
                  @JoinColumn(name = "bols", referencedColumnName = "BOLS"),
                  @JoinColumn(name = "eid", referencedColumnName = "EID")})
    @MapsId("projectId")
    private Project project;