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

时间:2017-03-27 08:53:21

标签: java mysql hibernate hibernate-mapping composite-primary-key

我有一张包含3个主键的表。它们是[ "id": "08c975bc-c4d8-4e90-a4b2-bdb2cd69e9e8", "event": { "id": "6625dc74-dc36-4a22-9fac-c63e96fe6049", "event_name": "Test Event", "event_location": "TVM, IND" }, "appointment_date": "2017-06-26 18:30:00" ] custom_idpatient_idpatient。我目前的映射如下所示

service_provider_type_idservice_provider_type

我的SQL代码

<?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>

如何映射这3个复合键?

1 个答案:

答案 0 :(得分: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>

或使用样式注释:

搜索@EmbeddedId

你可以在这里阅读更多细节(simmilar问题): 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
  }
}