【发布时间】:2017-08-19 21:28:48
【问题描述】:
我有一个包含 3 个主键的表。他们是custom_id、patient_idpatient、service_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 个复合键?
【问题讨论】:
-
你真的是指多个主键吗?还是您的意思是复合主键?我认为不可能有多个 pkey....
-
patient_idpatient和service_provider_type_idservice_provider_type不是 service_provider 的主键,它们是该表中的外键,因为它们是其他表的主键跨度> -
@Ben:好的,所以我是通过
mysql work bench创建的。是的,我认为您是正确的,请检查更新。
标签: java mysql hibernate hibernate-mapping composite-primary-key