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.  [ 3 posts ] 
Author Message
 Post subject: association mapping problem
PostPosted: Wed Aug 06, 2008 10:53 am 
Newbie

Joined: Wed Aug 06, 2008 10:38 am
Posts: 2
I have problem with assiciations for the following relations:

a person can purchase many items.
so records in purchase table can be like following:

person1, item1, qty,amt, time
person1,item2,qty,amt,time
.....



CREATE TABLE person (
person_id bigint(10) NOT NULL,
person_name varchar(50) default NULL
PRIMARY KEY (person_id)
)
CREATE TABLE item (
item_id bigint(10) default NULL,
item_name varchar(100) default NULL,
item_price double(10,2) default NULL )
CREATE TABLE purchase (

purchase_id bigint(10) NOT NULL default '0',
person_id bigint(10) NOT NULL default '0', item_id bigint(10) default NULL, quantity int(10) default NULL, amount double(10,2) default NULL, date_purchase datetime default NULL PRIMARY KEY (`purchase_id`)
)

i need to insert records into the purchase table and i need to retriview the information from the table. Please suggest me key mappings in hbm files.
i am stuck with creating hbms. Please help me in understanding this scenario.

Thanks in advance,

_________________
-Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 11:56 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
You need to have classes corresponding to Person, Item and Purchase. Assuming that you will not be accessing purchases from an item, I guess you need something like:

<class name="Person" table="PERSON">
<id name="id" type="java.lang.Long" column="PERSON_ID" ></id>
<property name="name" column="PERSON_NAME" type="string"/>
<set name="purchases">
<key>
<column name="PERSON_ID"/>
</key>
<one-to-many class="myData.Purchase"/>
</set>
</class>
<class name="Item" table="ITEM">
<id name="id" type="java.lang.Long" column="ITEM_ID" ></id>
<property name="name" column="ITEM_NAME" type="string"/>
<property name="price" column="ITEM_PRICE" type="double"/>
</class>
<class name="Purchase" table="PURCHASE">
<id name="id" type="java.lang.Long" column="PURCHASE_ID" ></id>
<property name="quantity" column="QUANTITY" type="int"/>
<property name="amount" column="AMOUNT" type="double"/>
<property name="purchaseDate" column="DATE_PURCHASE" type="timestamp"/>
<many-to-one name="person" column="PERSON_ID" class="myData.Person" not-null="true"/>
<many-to-one name="item" column="ITEM_ID" class="myData.Item" not-null="true"/>
</class>


Refer to hibernate documentation for more information on mapping and associations.

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 3:08 am 
Newbie

Joined: Wed Aug 06, 2008 10:38 am
Posts: 2
Thanks for the help...
i got good understanding of mappings for transaction tables like purchase.

_________________
-Naresh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.