[英]How to implement Composite Primary key and Composite Foreign Key using JPA,Hibernate, Springboot
我為此問題進行了大量搜索,但沒有找到任何具體解決方案。 我在一個表中有一個復合主鍵,此復合主鍵中的一個字段是另一張表的復合主鍵的一部分。 您可以說該特定字段是第二個表中的外鍵,但是在表定義中未定義任何排他性外鍵約束。 在第二個表中,對於第一個表中的每個記錄,可能有多個記錄。我正在嘗試使用SPringBoot-JPA-Hibernate來實現此功能,但無法這樣做。 可以幫我一下嗎? 這是detais:-
我有一個帶有以下字段的USER_CREDENTIAL表:-
CREATE TABLE `INSTITUTION_USER_CREDENTIAL` (
`INSTITUTION_USER_ID INT(10) NOT NULL, -> AutoGeneratd
`INSTITUTION_USER_NAME` VARCHAR(50) NOT NULL,
`INSTITUTION_USER_PASSWORD` VARCHAR(50) NOT NULL,
`FIRST_NAME` VARCHAR(100) NOT NULL,
`MIDDLE_NAME` VARCHAR(100),
`LAST_NAME` VARCHAR(100) NOT NULL,
PRIMARY KEY (`INSTITUTION_USER_ID`,`INSTITUTION_USER_NAME`)
);
2)這是我的第二張桌子
CREATE TABLE `INSTITUTION_USER_CREDENTIAL_MASTER` (
`INSTITUTION_ID` INT(10) NOT NULL, -> Autogenerated
`INSTITUTION_USER_ID` INT(10) NOT NULL, -> Coming from
INSTITUTION_USER_CREDENTIAL
`INSTITUTION_USER_ROLE` CHAR(02) NOT NULL,
`INSTITUTION_USER_STATUS` CHAR(02) NOT NULL,
`INSTITUTION_NAME` VARCHAR(200) NOT NULL,
`LAST_UPDT_ID` VARCHAR(100) NOT NULL,
`LAST_UPDT_TS` DATETIME NOT NULL,
PRIMARY KEY(`INSTITUTION_ID`,`INSTITUTION_USER_ID`,`INSTITUTION_USER_ROLE`)
);
請注意,我沒有在第二張表中聲明任何特定的外鍵。 我有兩個@Embeddable Class對應於兩個不同表的兩個主鍵結構:
對於INSTITUTION_USER_CREDENTIAL表:
@Embeddable
public class InstitutionUserCredentialPrimaryKey implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name = "INSTITUTION_USER_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int institutionUserId;
@Column(name = "INSTITUTION_USER_NAME")
private String institutionUserName;
//Getter-Setters removed for clarity
}
對應實體類:
@Entity(name = "INSTITUTION_USER_CREDENTIAL")
public class InstitutionUserCredential {
@EmbeddedId
private InstitutionUserCredentialPrimaryKey
institutionUserCredentialPrimaryKey;
@Column(name = "INSTITUTION_USER_PASSWORD")
private String instituteUserPassword;
@Column(name = "FIRST_NAME")
private String firstname;
@Column(name = "MIDDLE_NAME")
private String middleName;
@Column(name = "LAST_NAME")
private String lastName;
@OneToMany(mappedBy="institutionUserCredential", cascade = CascadeType.ALL)
private List<InstitutionUserCredentialMaster>
institutionUserCredentialMaster;
//Getter-Setter and other part of the code removed for clarity
}
對於INSTITUTION_USER_CREDENTIAL_MASTER表:
@Embeddable
public class InstituteUserCredentialMasterPrimaryKey implements Serializable
{
private static final long serialVersionUID = 1L;
@Column(name = "INSTITUTION_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int institutionId;
@Column(name = "INSTITUTION_USER_ID")
private int institutionUserId;
@Column(name = "INSTITUTION_USER_ROLE")
private String userRole;
//Getter-Setter and other part of the code removed for clarity
}
實體類別:-
@Entity(name = "INSTITUTION_USER_CREDENTIAL_MASTER")
public class InstitutionUserCredentialMaster {
@EmbeddedId
private InstituteUserCredentialMasterPrimaryKey
instituteUserCredentialMasterPrimaryKey;
@Column(name = "INSTITUTION_USER_STATUS")
private String userStatus;
@Column(name = "INSTITUTION_NAME")
private String institutionName;
@Column(name = "LAST_UPDT_ID")
private String lastUpdateId;
@Column(name = "LAST_UPDT_TS")
private String lastUpdateTimestamp;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="institutionUserId", referencedColumnName =
"INSTITUTION_USER_ID")
})
private InstitutionUserCredential institutionUserCredential;
//Getter-Setter and other part of the code removed for clarity
}
請注意,只有1個字段INSTITUTION_USER_ID在InstitutionUserCredentialMaster的組合主鍵中使用,並且來自InstitutionUserCredential的組合主鍵。
當我運行我的代碼時,這給了我一個錯誤:-
Invocation of init method failed; nested exception is
org.hibernate.AnnotationException:
referencedColumnNames(INSTITUTION_USER_ID) of com.bnl.application.entity.InstitutionUserCredentialMaster.institutionUserCredential referencing com.bnl.application.entity.InstitutionUserCredential not mapped to a single property
到目前為止,我所看到的涉及“復合主鍵”和“外鍵”的示例中沒有一個沒有處理任何特定字段,而是更多地涉及整個鍵結構。 我正在使用MYSQL,我檢查了是否可以創建具有復合主鍵的表,並且該復合鍵中的字段之一是另一個表中的外鍵,也是第二個表的復合主鍵的一部分。
任何指針贊賞
更新:-在我的第一篇文章中,我在發布它時犯了一個錯誤。 抱歉, institutionUserName
成為InstitutionUserCredentialMaster
的一部分。 這是一個錯字。 InstitutionUserCredentialMaster
表中不存在intitutionUserName
。 我已經修復並更新了帖子。
*****根據Niver和Wega的輸入進行更新*****
更新到InstitutionUserCredentialMasterPrimaryKey
@Embeddable
公共類InstituteUserCredentialMasterPrimaryKey實現了Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "INSTITUTION_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int institutionId;
@Column(name = "INSTITUTION_USER_ID")
private int institutionUserId;
// Added the institutionUserName
@Column(name = "INSTITUTION_USER_NAME")
private String institutionUserName;
@Column(name = "INSTITUTION_USER_ROLE")
private String userRole;
}
更新到實體類InsstitutionUserCredentialMaster:-
@Entity(name = "INSTITUTION_USER_CREDENTIAL_MASTER")
公共類InstitutionUserCredentialMaster {
@EmbeddedId
private InstituteUserCredentialMasterPrimaryKey instituteUserCredentialMasterPrimaryKey;
@Column(name = "INSTITUTION_USER_STATUS")
private String userStatus;
@Column(name = "INSTITUTION_NAME")
private String institutionName;
@Column(name = "LAST_UPDT_ID")
private String lastUpdateId;
@Column(name = "LAST_UPDT_TS")
private String lastUpdateTimestamp;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="institutionUserId", referencedColumnName = "INSTITUTION_USER_ID"),
@JoinColumn(name="institutionUserName",referencedColumnName = "INSTITUTION_USER_NAME")
})
private InstitutionUserCredential institutionUserCredential;
}
這次我遇到了類似的錯誤
Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Table [institution_user_credential_master] contains physical column name [institution_user_id] referred to by multiple physical column names: [institutionUserId], [INSTITUTION_USER_ID]
我認為問題在於您沒有在JoinColumns
批注中引用EmbeddedId
的另一部分。 您已經定義了institutionUserName
也是主鍵的一部分,因此在實體InstitutionUserCredentialMaster
的外鍵定義中也應提及它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.