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

Java 如何在Hibernate中映射多个复合键?,java,mysql,hibernate,hibernate-mapping,composite-primary-key,Java,Mysql,Hibernate,Hibernate Mapping,Composite Primary Key,我有一个有3个主键的表。它们是自定义id、患者id、服务提供商类型、服务提供商类型。我当前的映射如下所示 <?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"> <

我有一个有3个主键的表。它们是自定义id、患者id、服务提供商类型、服务提供商类型。我当前的映射如下所示

<?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个组合键?

您要查找的是一个组合键,而不是多个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>
这可能是Id类:未测试!:


您正在寻找一个复合密钥,而不是多个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>
这可能是Id类:未测试!:


你真的是指多个主键吗?还是说复合主键?我认为不可能有多个pkey…patient\u idpatient和service\u provider\u type\u idservice\u provider\u type不是service\u provider的主键,它们是该表中的外键,因为它们是其他数据库的主键tables@Ben:好的,我是通过mysql工作台创建的。是的,我认为你是正确的,请检查更新。你真的是指多个主键吗?还是说复合主键?我认为不可能有多个pkey…patient\u idpatient和service\u provider\u type\u idservice\u provider\u type不是service\u provider的主键,它们是该表中的外键,因为它们是其他数据库的主键tables@Ben:好的,我是通过mysql工作台创建的。是的,我想你是对的,请检查更新。谢谢。你能把代码添加到我给定的映射代码中来编辑它吗?在添加代码之前,我找不到是否应该删除已完成的关系映射,是否应该删除已完成的主键映射等等。@PeakGen请参阅我的编辑。你只要试一下看一下文档,这很简单…谢谢。你能把代码添加到我给定的映射代码中来编辑它吗?在添加代码之前,我找不到是否应该删除已完成的关系映射,是否应该删除已完成的主键映射等等。@PeakGen请参阅我的编辑。你只要试着读一下文档,这很容易。。。
<?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>
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
  }
}