-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Mapping Composite Key using Hibernate:Help
PostPosted: Mon Aug 29, 2005 7:52 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
Hi,
I am using hibernate 2.0. I would like to know how to Map composite Key using the Hibernate.
I am having one Database Table named RevenueAttributeLink in that I am having two Columns "revenueId" and "revenueattributeId" these two are composite key.
My hibernate mapping XML I have written like this.I am using this Table to read the values returned from this table.

*****************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="au.com.combined.domain.revenue.RevenueAttributeLinkBusinessObject" table="RevenueAttributeLink">
<composite-id>
<key-property name="revenueID" type="long" column="revenueID" unsaved-value="0" />
<key-property name="revenueAttributeID" type="long" column="revenueID" unsaved-value="0" />
<generator class="assinged">
</generator>
</composite-id>
</class>
</hibernate-mapping>
*********************************************************
After running I am getting following errors.Please let me know if I need to modify the Mapping XML.
*************************************************

[29/08/05 21:51:39:426 EST] 11d711d7 XMLHelper E net.sf.hibernate.util.XMLHelper Error parsing XML: XML InputStream(8) Attribute "unsaved-value" must be declared for element type "key-property".

[29/08/05 21:51:39:442 EST] 11d711d7 XMLHelper E net.sf.hibernate.util.XMLHelper Error parsing XML: XML InputStream(9) Attribute "unsaved-value" must be declared for element type "key-property".

[29/08/05 21:51:39:442 EST] 11d711d7 XMLHelper E net.sf.hibernate.util.XMLHelper Error parsing XML: XML InputStream(12) The content of element type "composite-id" must match "(meta*,(key-property|key-many-to-one)+)".
ERROR [endeavour.core ] HibernateSessionFactory::<clinit>: Problem building hibernate session factory
net.sf.hibernate.MappingException: Error reading resource: au/com/combined/persistence/billing/RevenueAttributeLink.hbm.xml

**************************************************************


Top
 Profile  
 
 Post subject: Re: Mapping Composite Key using Hibernate:Help
PostPosted: Mon Aug 29, 2005 8:05 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
Remove the generator element.

Composite keys can ONLY be assigned, so you don’t need to instruct Hibernate this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 8:49 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
It didn't solve the problem..I am getting the Same Error.


org.xml.sax.SAXParseException: Attribute "unsaved-value" must be declared for element type "key-property".


Top
 Profile  
 
 Post subject: unsaved-value
PostPosted: Mon Aug 29, 2005 9:02 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
The unsaved-value can only be "undefined|any|none"

See:
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-compositeid


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 9:08 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
I have set it to "none".
Will it help me while updating the table.If I want to insert the data into the same table.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 9:30 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
We are using composite keys in our solution, and we don’t specify any unsaved-value in our maps (default is undefined). The application is designed to always assign a value to the object before it is passed on to Hibernate for persisting.

Was that an answer to your question??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 10:07 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
Yeah after removing that worked...But now I am getting Classcast exception...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 10:58 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
I am getting folowing Exception
*************************************************
[30/08/05 00:50:42:585 EST] 1ce11ce1 SystemErr R au.com.combined.core.SystemException: java.lang.ClassCastException: java/lang/Long incompatible with au/com/combined/domain/revenue/RevenueAttributeLinkBusinessObject
[30/08/05 00:50:42:648 EST] 1ce11ce1 SystemErr R at java.lang.Throwable.<init>(Throwable.java:59)
[30/08/05 00:50:42:648 EST] 1ce11ce1 SystemErr R at java.lang.Throwable.<init>(Throwable.java:104)
[30/08/05 00:50:42:648 EST] 1ce11ce1 SystemErr R at au.com.combined.core.WrappedMessageException.<init>(WrappedMessageException.java:76)
[30/08/05 00:50:42:648 EST] 1ce11ce1 SystemErr R at au.com.combined.core.SystemException.<init>(SystemException.java:60)
[30/08/05 00:50:42:648 EST] 1ce11ce1 SystemErr R at au.com.combined.persistence.HibernatePersistenceImpl.read(HibernatePersistenceImpl.java)
*************************************************************
Throwing Error on this particular line in the code.

BusinessObject object = (BusinessObject) session.get(klass, new Long(id));

It is getting the klass as a RevenueAttributeLinkBusinessObject.class and
new Long(id) as 4449.

Do I need to modify my Mapping Xml or business object.the business object is having equals and hashcode methods in it like this.
**********************************
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RevenueAttributeLinkBusinessObject)) {
return false;
}
RevenueAttributeLinkBusinessObject that = RevenueAttributeLinkBusinessObject) o;
return(this.revenueID == that.revenueID);
}
*************************************
public int hashCode() {
HashCode hash = new HashCode();
hash.add(revenueID);
hash.add(revenueAttributeID);
return hash.hashCode();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 11:11 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
I don’t think your can query the way you are doing (Take a look at the API for sessiong.get(....))

We are using criteria which would give something like this:
Code:
BusinessObject object = (BusinessObject) session.createCriteria(klass).add(Restrictions.eq("revenueID", new Long(id)).uniqueResult();


Good luck


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 1:07 am 
Newbie

Joined: Mon Aug 08, 2005 5:48 am
Posts: 9
Just want to know weather my Mapping Xml is right or
I have to change it
*************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="au.com.combined.domain.revenue.RevenueAttributeLinkBusinessObject" table="biRevenueAttributeLink">
<composite-id>
<key-property name="revenueID" type="long" column="revenueID" />
<key-property name="revenueAttributeID" type="long" column="revenueAttributeID" />
</composite-id>
</class>
</hibernate-mapping>
****************************************************
Equal and Hash code implementation in my Business Object.

****************************************************

public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof RevenueAttributeLinkBusinessObject)) {
return false;
}
RevenueAttributeLinkBusinessObject that = (RevenueAttributeLinkBusinessObject) o;
return(this.revenueID == that.revenueID && this.revenueAttributeID == that.revenueAttributeID);

}
****************************************************
public int hashCode() {
HashCode hash = new HashCode();
hash.add(revenueID);
hash.add(revenueAttributeID);
return hash.hashCode();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.