gpt4 book ai didi

java - 如何在 Hibernate 中映射多个复合键?

转载 作者:行者123 更新时间:2023-11-29 04:16:31 24 4
gpt4 key购买 nike

我有一个包含 3 个主键的表。它们是 custom_idpatient_idpatientservice_provider_type_idservice_provider_type。我当前的映射如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.ServiceProvider" table="service_provider" catalog="myglukose" optimistic-lock="version">
<id name="customId" type="string">
<column name="custom_id" not-null="true"/>
</id>
<many-to-one name="patient" class="beans.Patient" fetch="select">
<column name="patient_idpatient" not-null="true"/>
</many-to-one>
<many-to-one name="serviceProviderType" class="beans.ServiceProviderType" fetch="select">
<column name="service_provider_type_idservice_provider_type" not-null="true"/>
</many-to-one>
<property name="dateCreated" type="timestamp">
<column name="date_created" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="last_updated" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>

我的SQL代码

CREATE TABLE IF NOT EXISTS `xxx`.`service_provider` (
`custom_id` VARCHAR(45) NOT NULL,
`patient_idpatient` INT NOT NULL,
`service_provider_type_idservice_provider_type` INT NOT NULL,
`date_created` TIMESTAMP NULL,
`last_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX `fk_service_provider_patient1_idx` (`patient_idpatient` ASC),
INDEX `fk_service_provider_service_provider_type1_idx` (`service_provider_type_idservice_provider_type` ASC),
PRIMARY KEY (`custom_id`, `patient_idpatient`, `service_provider_type_idservice_provider_type`),
CONSTRAINT `fk_service_provider_patient1`
FOREIGN KEY (`patient_idpatient`)
REFERENCES `xxx`.`patient` (`idpatient`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_service_provider_service_provider_type1`
FOREIGN KEY (`service_provider_type_idservice_provider_type`)
REFERENCES `xxx`.`service_provider_type` (`idservice_provider_type`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

如何映射这 3 个复合键?

最佳答案

您要查找的是复合 key ,而不是多个 PKey。

这在 Hibernate 中看起来像这样(通过 hbm.xml):

<composite-id name="compId">
<key-property name="customId” column="custom_id" />
<key-property name="patientIdpatient" column="patient_idpatient" />
<key-property name="serviceProviderTypeIdserviceProviderType" column="service_provider_type_idservice_provider_type" />
</composite-id>

或者像样式这样的注释:

搜索@EmbeddedId

您可以在此处阅读更多详细信息(类似问题): How to map a composite key with Hibernate?

编辑
这是一个简单的代码示例,它(也许)可以帮助您指明正确的方向:

您的 hbm.xml 可能看起来像(未经测试,如果不进行更正,代码可能无法工作!):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.ServiceProvider" table="service_provider" catalog="myglukose" optimistic-lock="version">

<composite-id name="myId">
<key-property name="customId” column="custom_id" />
<key-property name="patientIdpatient" column="patient_idpatient" />
<key-property name="serviceProviderTypeIdserviceProviderType" column="service_provider_type_idservice_provider_type" />
</composite-id>

<!-- not sure with this part...maybe not needed -->
<many-to-one name="patient" class="beans.Patient" fetch="select">
<column name="patient_idpatient" not-null="true"/>
</many-to-one>

<!-- not sure with this part...maybe not needed -->
<many-to-one name="serviceProviderType" class="beans.ServiceProviderType" fetch="select">
<column name="service_provider_type_idservice_provider_type" not-null="true"/>
</many-to-one>

<property name="dateCreated" type="timestamp">
<column name="date_created" length="19" />
</property>

<property name="lastUpdated" type="timestamp">
<column name="last_updated" length="19" not-null="true" />
</property>

</class>
</hibernate-mapping>

这可能是 Id-Class:(未测试!):

import java.io.Serializable;

public class MyId implements Serializable {
private beans.ServiceProviderType spt;
private int customId;
private beans.Patient pat;

// an easy initializing constructor
public MyId(int customId, beans.Patient pat, beans.ServiceProviderType spt) {
this.pat = pat;
this.customId = customId;
this.spt = spt;
}

public beans.Patient getPatient() {
return pat;
}

public void setPatient(beans.Patient pat) {
this.pat = pat;
}

public beans.ServiceProviderType getServiceProviderType() {
return spt;
}

public void setServiceProviderType(beans.ServiceProviderType pat) {
this.spt = spt;
}

public int getCustomId() {
return customerId;
}

public void setCustomId(int customId) {
this.customId = customId;
}

@Override
public boolean equals(Object arg0) {
//needs implementation
}

@Override
public int hashCode() {
//needs implementation
}
}

关于java - 如何在 Hibernate 中映射多个复合键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com