如何使用JPA,Hibernate,Springboot实现Composite主键和Composite外键

时间:2018-06-03 19:17:43

标签: java mysql hibernate spring-boot jpa

我搜索了很多这个特殊的问题,但我没有找到任何具体的解决方案。我在一个表中有一个复合主键,该复合主键中的一个字段是另一个表的复合主键的一部分。您可以说这个特定字段是第二个表中的外键,但我没有在表定义中定义任何独占的外键约束。对于第一个表中的每个rec,第二个表中可以有多个记录。我正在尝试使用SPringBoot-JPA-Hibernate实现此目的但不能这样做。有人可以帮助我。以下是详情: -

我有一个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
}

请注意,InstitutionUserCredentialMaster的Composite PrimaryKey中只使用了1个字段INSTITUTION_USER_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,我已经检查过我们可以创建具有复合主键的表,并且该复合键中的一个字段是另一个表中的外键,也是第二个表的Composite Primary键的一部分。

赞赏任何指针

更新: - 在我的第一篇文章中,我在发布时犯了一个错误。很抱歉institutionUserName成了InstitutionUserCredentialMaster的一部分。这是一个错字。 intitutionUserName表中不存在InstitutionUserCredentialMaster。我已修复并更新了帖子。

*****根据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")

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"),
    @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 个答案:

答案 0 :(得分:1)

我认为问题在于您没有引用EmbeddedId注释中JoinColumns的其他部分。您已经定义了institutionUserName也是主键的一部分,因此您应该在实体InstitutionUserCredentialMaster中的外键定义中提及它。