Hibernate + composite primary keys


General J2EE: Hibernate + composite primary keys

  1. Hibernate + composite primary keys (6 messages)


     I am trying to use hibernate to fetch data from a database table which does'nt have a primary key defined. However the fact is 3 fields of that table together are unique in the table.

     I am not able to figure out how to represent this in the hibernate mapping table for that class. I read that there is a <composite-primarykey..> tag to do this. Tried a few combinations but failed to fetch the entire data from the table.

     Could someone please throw light on this and the way xxx.hbm.xml file should be defined in this sceneario.


    Threaded Messages (6)

  2. Example of a composite key mapping[ Go to top ]

           <!-- applicationPK is another class the implements Serializable -->

            class="org.example.composite.ApplicationPK" >
            <key-property name="collectorateCode" column="COLLECTORATE_CODE" type="string" />
            <key-property name="applicationNumber" column="APPL_SNO" type="integer"/>
            <key-property name="year" column="YEAR_NBR" type="integer"/>
          <!-- Normal properties of a java class -->
           <property name="name" column="NAME" type="stringr"></property>
           <property name="age" column="AGE" type="integer"></property>

    Here is an example of the Composite key class
    public class Application {
           private ApplicationPK applicationPK;
           //declare other properties
           //declare constructor
    public class ApplicationPK implements Serializable {
            //declare composite properties
            // declare constructor


    How to call the query
    Note you need to set the required setter method in the composite key class
  3. Example of a composite key mapping[ Go to top ]

    Thank you so much.. ur suggestion works :-)
  4. Example of a composite key mapping[ Go to top ]


     I am trying to create a hibernate mapping file for a table with out any primary key or unique fields[ the table can have any field data duplicated ]. Seems like Hibernate does'nt allow us to create a mapping file with out some unique data.

     Any help to solve this problem is greatly appreciated

  5. Hi,

    Iam getting the following error

     java.lang.RuntimeException: net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.bofa.compasst.dao.CtBankruptcyCode.cusBnkrptChapIn

    when I tried your soln.

    Here is my mapping file,PK class

    package com.bofa.compasst.dao;

    import java.io.Serializable;

    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;

    public class CtBankruptcyCodePK implements Serializable
       /** identifier field */
       public String cusBnkrptChapIn;

        /** identifier field */
        public String cusBnkrptStatCd;
        public CtBankruptcyCodePK() {

        public void setCusBnkrptChapIn(String cusBnkrptChapIn) {
         this.cusBnkrptChapIn = cusBnkrptChapIn;
        public void setCusBnkrptStatCd(String cusBnkrptStatCd) {

         this.cusBnkrptStatCd = cusBnkrptStatCd;

        public String getCusBnkrptChapIn() {
         return this.cusBnkrptChapIn;
        public String getCusBnkrptStatCd() {

         return this.cusBnkrptStatCd;

        public boolean equals(Object other) {
            if ( !(other instanceof CtBankruptcyCodePK) ) return false;
            CtBankruptcyCodePK castOther = (CtBankruptcyCodePK) other;
            return new EqualsBuilder()
                .append(this.getCusBnkrptChapIn(), castOther.getCusBnkrptChapIn())
                .append(this.getCusBnkrptStatCd(), castOther.getCusBnkrptStatCd())

        public int hashCode() {
            return new HashCodeBuilder()

        Created by the Middlegen Hibernate plugin 2.1



        <composite-id name="ctBankruptcyCodePK" class="com.bofa.compasst.dao.CtBankruptcyCodePK">


        <!-- Associations -->
        <!-- derived association(s) for compound key -->
        <!-- end of derived association(s) -->

  6. Composite key issue[ Go to top ]

    what do you mean by

    Note you need to set the required setter method in the composite key class

    I get the following error
    net.sf.hibernate.MappingException: Error reading resource: com/auctioninfo/model/Person.hbm.xml
    at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:325)
    at com.manheim.auctioninfo.dao.AuctionInfoDAO.prepareAuctionInfoConfiguration(AuctionInfoDAO.java:21)
    at com.auctioninfo.dao.AuctionInfoDAO.findAuctionInfo(AuctionInfoDAO.java:31)
    at com.test.HibernateTest.testAuctionInfo(HibernateTest.java:23)
    at java.lang.reflect.Method.invoke(Native Method)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:329)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:218)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:151)
    Caused by: net.sf.hibernate.MappingException: composite-id class must override equals() and hashCode(): com.auctioninfo.model.PersonPK
    at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:295)
    at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1204)
    at net.sf.hibernate.cfg.Configuration.add(Configuration.java:247)
    at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:281)
    at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:322)
    ... 15 more

    PersonPK has set/get methods in Person class.

    public class PersonPK implements Serializable
    public PersonPK(){}

    private String PRTFNAME;
    private String PRTLNAME;

    public String getPRTFNAME(){return PRTFNAME;}
    public String getPRTLNAME(){return PRTLNAME;}
    public void setPRTFNAME(String pRTFNAME){PRTFNAME = pRTFNAME;}
    public void setPRTLNAME(String pRTLNAME){PRTLNAME = pRTLNAME;}

    My mapping
    <class name="com.auctioninfo.model.Person" table="PRTDIRVIEW">
    <composite-id name="personName" class="com.auctioninfo.model.PersonPK">
    <key-property name="PRTLNAME"/>
    <key-property name="PRTFNAME"/>
    <property name="AUCTID">
    <column name="AUCTID" />
    <property name="FAX">
    <column name="FAX" />
    <property name="PHONE">
    <column name="PHONE" />
    <property name="PSEMLADDR">
    <column name="PSEMLADDR" />
    <property name="PSDEPTDESC">
    <column name="PSDEPTDESC" />
    <property name="PRTFLAG">
    <column name="PRTFLAG" />
    <property name="PRTEMPST">
    <column name="PRTEMPST"/>

    Please reply. thanks
  7. Hibernate + composite primary keys[ Go to top ]

    Can anyone help me out how to write hql ( geting max value of a variable )for those POJOs which are having composite key in their corresponding hbm file. thanks,