import java.math.BigDecimal;
import java.util.Date;
public class Shipment {
private Shipmentgeo shipmentgeo;
private ShipmentPK pk;
// implement getter and setter methods of pk
private String shipper;
private String consignee;
private String lastupdby;
// Constructors
/** default constructor */
public Shipment() {
}
/** full constructor */
public Shipment(String shipper, String consignee) {
this.shipper = shipper;
this.consignee = consignee;
}
public String getShipper() {
return this.shipper;
}
public void setShipper(String shipper) {
this.shipper = shipper;
}
public String getConsignee() {
return this.consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public void setLastupdby(String lastupdby) {
this.lastupdby = lastupdby;
}
public Shipmentgeo getShipmentgeo() {
return shipmentgeo;
}
public void setShipmentgeo(Shipmentgeo shipmentgeo) {
this.shipmentgeo = shipmentgeo;
}
}
public class ShipmentPK implements Serializable {
private String shipref;
private String traffictype;
public String getShipref() {
return shipref;
}
public void setShipref(String shipref) {
this.shipref = shipref;
}
public String getTraffictype() {
return traffictype;
}
public void setTraffictype(String traffictype) {
this.traffictype = traffictype;
}
public boolean equals(Object that) {
if (!(that instanceof ShipmentPK))
return false;
ShipmentPK ship = (ShipmentPK) that;
return ship.getShipref().equals(shipref)
&& ship.getShipref().equals(traffictype);
}
public int hashCode() {
return shipref.hashCode() + traffictype.hashCode();
}
}
import java.util.Date;
public class Shipmentgeo implements java.io.Serializable {
private ShipmentPK pk;
// implement getter and setter methods of pk
private Shipment shipment;
private String departure;
private String arrival;
// Constructors
/** default constructor */
public Shipmentgeo() {
}
/** minimal constructor */
/** full constructor */
public Shipmentgeo(String departure, String arrival) {
this.departure = departure;
this.arrival = arrival;
}
public String getDeparture() {
return this.departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getArrival() {
return this.arrival;
}
public Shipment getShipment() {
return shipment;
}
public void setShipment(Shipment shipment) {
this.shipment = shipment;
}
}
Shipment.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Shipment" table="shipment"
schema="informix">
<composite-id name="pk" class="ShipmentPK">
<key-property name="shipref" type="string">
<column name="shipref" length="15" />
</key-property>
<key-property name="traffictype" type="string">
<column name="traffictype"/>
</key-property>
</composite-id>
<property name="shipper" type="string">
<column name="shipper" length="3" />
</property>
<property name="consignee" type="string">
<column name="consignee" length="3" />
</property>
<one-to-one name="shipmentgeo" class="Shipmentgeo" />
</class>
</hibernate-mapping>
///////////
Shipmentgeo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Shipmentgeo" table="shipmentgeo"
schema="informix">
<composite-id name="pk" class="ShipmentPK">
<key-property name="shipref" type="string">
<column name="shipref" length="15" />
</key-property>
<key-property name="traffictype" type="string">
<column name="traffictype" />
</key-property>
</composite-id>
<property name="departure" type="string">
<column name="departure" length="3" />
</property>
<property name="arrival" type="string">
<column name="arrival" length="3" />
</property>
</class>
<one-to-one name="shipment" class="Shipment" />
</class>
</hibernate-mapping>
Hi Guys, this is the solution of one-to-one relation between two tables having composite key on both the tables. This is working well.
The approach to use same persitence class as a composite-id is not good performance. use another class for composite-id as shown above.
For any clarification you can contact me on
rajavijaykumar@gmail.com
Hope this will help a lot you, All the best :-)