Apurv
Apurv

Reputation: 3753

Hibernate mapping for composite key

I am trying to insert records to a table using hibernate. The table structure is like below

CREATE TABLE MYTABLE 
(   
    TRADE_ID NUMBER(18,0) NOT NULL, 
    EFFECTIVE_DATE DATE NOT NULL, 
    AMOUNT BINARY_DOUBLE NOT NULL ENABLE, 
    CREATED_TS TIMESTAMP (6), 
    UPDATED_TS TIMESTAMP (6), 
    CONSTRAINT MYTABLE_PK PRIMARY KEY ("TRADE_ID", "EFFECTIVE_DATE")
) ;

Could yo please help me with the mapping xml for the composite key ?

Upvotes: 1

Views: 86

Answers (1)

Apostolos
Apostolos

Reputation: 10498

Try something like this in your mytable.hbm.xml file.

  <composite-id name="id" class="MyCompositeKeyClass">
     <key-property name="tradeId" column="TRADE_ID" type="integer"/>
     <key-property name="effectiveDate" column="EFFECTIVE_DATE" type="date"/>
  </composite-id>

Then in your MyCompositeKeyClass class you can define the tradeId and effectiveDate fields.

public class MyCompositeKeyClass implements Serializable {
   private Integer tradeId;
   private Date effectiveDate;

   public MyCompositeKeyClass() {
   }
   // setters, getters
}

and in your MyTable class you should add a field

private MyCompositeKeyClass id;

Upvotes: 1

Related Questions