jpa如何使来自两个不同表的复合外键成为单个表的复合主键的一部分

sbtkgmzw  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(350)

我的表格结构如下:

Table Campaign{
Camp_id(PK),
other columns...
}

Table User{
user_id(PK),
other columns...
}

Table Candidate{
cand_id(PK),
other columns...
}

Table Result{
user_id(PK),
camp_id(PK),
cand_id
}
sf6xfgos

sf6xfgos1#

试着这样做:
实体表格活动

@Entity
@Table(name="CAMPAIGN")
public class Campaign implements Serializable {

    @GeneratedValue(...)
    @Id
    @Column(name="CAMP_ID")
    Long campId
    ....
}

实体用户

@Entity
@Table(name="USER")
public class User implements Serializable {

    @GeneratedValue(...)
    @Id
    @Column(name="USER_ID")
    Long userId
    ....
}

实体候选人

@Entity
@Table(name="CANDIDATE")
public class Candidate implements Serializable {

    @GeneratedValue(...)
    @Id
    @Column(name="CAND_ID")
    Long candId
    ....
}

实体结果

@Entity
@Table(name="RESULT")
public class Result implements Serializable {

    @EmbeddedId
    private ResultId id;

    @ManyToOne
    @JoinColumn(name = "CAND_ID")
    private Candidate candidate;
    ...
}

@Embeddable
public class ResultId implements Serializable {

    @ManyToOne
    @JoinColumn(name = "USER_ID")
    private User user;

    @ManyToOne
    @JoinColumn(name = "CAMP_ID")
    private Campaign campaign;

}

相关问题