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

Pea*_*Gen 1 java mysql hibernate hibernate-mapping composite-primary-key

我有一个包含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>
Run Code Online (Sandbox Code Playgroud)

我的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;
Run Code Online (Sandbox Code Playgroud)

如何映射这3个复合键?

Ben*_*Ben 5

您正在寻找一个复合键,而不是多个PKeys.

在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>
Run Code Online (Sandbox Code Playgroud)

或者像样式这样的注释:

搜索 @EmbeddedId

你可以在这里阅读更多细节(simmilar问题): 如何使用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>
Run Code Online (Sandbox Code Playgroud)

这可能是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
  }
}
Run Code Online (Sandbox Code Playgroud)