Reputation: 23035
I have a table with 3 primary keys. They are custom_id
, patient_idpatient
, service_provider_type_idservice_provider_type
. My current mapping is like below
<?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>
my SQL code
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;
How can I map these 3 composite keys?
Upvotes: 2
Views: 2708
Reputation: 3528
You are looking for a composite key, not multiple PKeys.
This can look like this in Hibernate (via 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>
or with Annotation like Style:
search for @EmbeddedId
You can read about more details e.g here (simmilar question): How to map a composite key with Hibernate?
EDIT
Here is a simple code example, which (maybe) can help you to show the correct direction:
Your hbm.xml can look like (not tested, code may not work without corrections!):
<?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>
and this could be the Id-Class: (not tested!):
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
}
}
Upvotes: 5