为了这个问题我找了很多,但没有找到任何具体的解决办法。我在一个表中有一个复合主键,这个复合主键中的一个字段是另一个表的复合主键的一部分。您可以说这个特定字段是第二个表中的外键,但我没有在表定义中定义任何排他外键约束。对于第一个表中的每个rec,第二个表中可能有多个记录,我正试图使用springboot jpa hibernate实现这一点,但无法做到。有人能帮帮我吗。这是你的名字detais:-
我有一个带有以下内容的用户凭证表fields:-
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) 这是我的第二张table
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类对应于两个不同的主键结构table:-
对于机构\用户\凭证table:-
@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
}
对应实体class:-
@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
}
对于机构\用户\凭证\主机table:-
@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
}
实体class:-
@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
}
请注意,在institutionusercredentialmaster的复合主键中只使用了一个字段institution\u user\u id,它来自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,我检查过是否可以创建一个具有复合主键的表,该复合主键中的一个字段是另一个表中的外键,也是第二个表的复合主键的一部分。
有什么建议吗
update:- in 我的第一篇文章我在发的时候犯了一个错误。我很抱歉 institutionUserName
成为世界的一部分 InstitutionUserCredentialMaster
. 那是个打字错误。不存在 intitutionUserName
在 InstitutionUserCredentialMaster
table。我已经修复并更新了帖子。
根据niver和wega的输入进行更新
更新到institutionusercredentialmasterprimarykey
@Embeddable
公共类instituteusercredentialmasterprimarykey实现可序列化{
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;
}
更新实体类InstitutionUserCredentialMaster:-
@Entity(name = "INSTITUTION_USER_CREDENTIAL_MASTER")
公共类机构用户证书管理员{
@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]
1条答案
按热度按时间mspsb9vt1#
我认为问题是你没有提到问题的另一部分
EmbeddedId
在JoinColumns
注解。你已经定义了institutionUserName
是主键的一部分,因此您应该在实体中外键的定义中也提到它InstitutionUserCredentialMaster
.