In Real world applications it is natural to  have composite keys  to identify a row, which is combination of key  columns.

Account Table

name type amount place houseno
Ajit savings 20000 MgRoad H002
Amar savings 30000 VictoriaRoad H003

Here  in "Account "table  you can create composite in combinations of “place “and “houseno” columns.

Following are the steps for achieving it

Step1: Create  Account  class

Account.java

{codecitation class="brush: java"}

package com.bullraider.cmpositekey;
import java.io.Serializable;
public class Account implements Serializable{
    private String place;
    private String  houseno;
    private String accounId;
    private String name;
    private String type;
    private int amount;
    public Account( String houseno,String place, String name,int amount,
            String type) {
        this.amount = amount;
        this.houseno = houseno;
        this.name = name;
        this.place = place;
        this.type = type;
    }
    public String getPlace() {
        return place;
    }
    public void setPlace(String place) {
        this.place = place;
    }
    public String getHouseno() {
        return houseno;
    }
    public void setHouseno(String houseno) {
        this.houseno = houseno;
    }
    public String getAccounId() {
        return accounId;
    }
    public void setAccounId(String accounId) {
        this.accounId = accounId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }

}

{/codecitation}

Step2: Create Account.hbm.xml

{codecitation class="brush: 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="com.bullraider.cmpositekey.Account" table="Account1">
        <meta attribute="class-description"> This class contains Account details. </meta>
        <composite-id>
            <key-property name="houseno" column="houseno" type="string">
            </key-property>
            <key-property name="place" column="place" type="string"></key-property>
        </composite-id>
        <property name="name" type="string" column="name" not-null="true" />
        <property name="type" type="string" column="job" not-null="true" />
        <property name="amount" type="integer" column="amount"
            not-null="true" />
    </class>
</hibernate-mapping>
{/codecitation}


Step3: Create Configuration file

{codecitation class="brush: xml"}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
       <property name="hbm2ddl.auto">create-drop</property>
        <mapping resource="com/bullraider/cmpk/Account.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

{/codecitation}
step4: Create HibernateUtil class to get SessionFactory Object

{codecitation class="brush: java"}
package com.bullraider.cmpositekey.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("SessionFactory creation failed" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

{/codecitation}
step5: Create Main Class for adding opertions.

{codecitation class="brush: java"}

package com.bullraider.cmpositekey;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bullraider.cmpositekey.util.HibernateUtil;

public class Main {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Account account1=new Account("H1001","MG ROAD","NEELESH",23330,"savings");
            Account account2=new Account("H1004","MG ROAD","NIRLIPTA",23330,"savings");
            session.save(account1);
            session.save(account2);
            transaction.commit();
            System.out.println("Records inserted sucessessfully");
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

    }
}


{/codecitation}