Hi,
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.
Thanks
-
Hibernate + composite primary keys (6 messages)
- Posted by: Kiran Kumar
- Posted on: November 28 2003 00:19 EST
Threaded Messages (6)
- Example of a composite key mapping by Howard Hill on November 28 2003 14:53 EST
- Example of a composite key mapping by Kiran Kumar on November 29 2003 11:29 EST
- Example of a composite key mapping by Kiran Kumar on December 01 2003 19:53 EST
- Getting PropertyAccess error when I tried your soln by kumar vishwanath on January 20 2005 09:20 EST
- Composite key issue by Raju Atchutuni on March 22 2004 11:27 EST
- Hibernate + composite primary keys by Prashant Sharma on September 05 2007 09:36 EDT
-
Example of a composite key mapping[ Go to top ]
- Posted by: Howard Hill
- Posted on: November 28 2003 14:53 EST
- in response to Kiran Kumar
<hibernate-mapping>
<class
name="org.example.composite.Application"
table="APPLICATION">
<!-- applicationPK is another class the implements Serializable -->
<composite-id
name="applicationPK"
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"/>
</composite-id>
<!-- Normal properties of a java class -->
<property name="name" column="NAME" type="stringr"></property>
<property name="age" column="AGE" type="integer"></property>
</class>
</hibernate-mapping>
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
ht.load(Application.class,applicationPK) -
Example of a composite key mapping[ Go to top ]
- Posted by: Kiran Kumar
- Posted on: November 29 2003 11:29 EST
- in response to Howard Hill
Thank you so much.. ur suggestion works :-) -
Example of a composite key mapping[ Go to top ]
- Posted by: Kiran Kumar
- Posted on: December 01 2003 19:53 EST
- in response to Howard Hill
Hi,
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
Thanks -
Getting PropertyAccess error when I tried your soln[ Go to top ]
- Posted by: kumar vishwanath
- Posted on: January 20 2005 09:20 EST
- in response to Kiran Kumar
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())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getCusBnkrptChapIn())
.append(getCusBnkrptStatCd())
.toHashCode();
}
}
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="com.bofa.compasst.dao.CtBankruptcyCode"
table="CT_BANKRUPTCY_CODE"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true"
schema="nbkm657"
>
<composite-id name="ctBankruptcyCodePK" class="com.bofa.compasst.dao.CtBankruptcyCodePK">
<key-property
name="cusBnkrptChapIn"
column="CUS_BNKRPT_CHAP_IN"
type="java.lang.String"
length="2"
>
</key-property>
<key-property
name="cusBnkrptStatCd"
column="CUS_BNKRPT_STAT_CD"
type="java.lang.String"
length="1"
>
</key-property>
</composite-id>
<property
name="accBnkrptIn"
column="ACC_BNKRPT_IN"
type="java.lang.String"
length="1"
>
</property>
<property
name="lstUpdateDt"
column="LST_UPDATE_DT"
type="java.sql.Timestamp"
length="7"
>
</property>
<property
name="lstUpdateByNm"
column="LST_UPDATE_BY_NM"
type="java.lang.String"
length="30"
>
</property>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->
</class>
</hibernate-mapping> -
Composite key issue[ Go to top ]
- Posted by: Raju Atchutuni
- Posted on: March 22 2004 11:27 EST
- in response to Howard Hill
what do you mean by
Note you need to set the required setter method in the composite key class
ht.load(Application.class,applicationPK)
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
<hibernate-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"/>
</composite-id>
<property name="AUCTID">
<column name="AUCTID" />
</property>
<property name="FAX">
<column name="FAX" />
</property>
<property name="PHONE">
<column name="PHONE" />
</property>
<property name="PSEMLADDR">
<column name="PSEMLADDR" />
</property>
<property name="PSDEPTDESC">
<column name="PSDEPTDESC" />
</property>
<property name="PRTFLAG">
<column name="PRTFLAG" />
</property>
<property name="PRTEMPST">
<column name="PRTEMPST"/>
</property>
</class>
</hibernate-mapping>
Please reply. thanks -
Hibernate + composite primary keys[ Go to top ]
- Posted by: Prashant Sharma
- Posted on: September 05 2007 09:36 EDT
- in response to Kiran Kumar
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,