-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can mention <id> tag for a foreign key in hbm.xml
PostPosted: Fri Jan 11, 2008 3:01 am 
Newbie

Joined: Tue Dec 18, 2007 1:27 am
Posts: 3
Here my application is relation between Item and Bid. There will be more than one bid for single item. I am explaining problem in detail below.

First table ITEM(Item_Id(primary key) Type Integer, Item_name Type String)

I have two table Second table BID (Bid_Id(primary key) Type Integer ,Item_Id(foreign key) Type Integer , Bid_name Type String )

I have used <many-to-one> tag in my bid.hbm.xml , Now i am able to access aBid.getItem, see the code shown below

[code]
Item item =new Item();
Bid bid=new Bid();
bid=(Bid)session.get(Bid.class, Bid_Id)
item=bid.getItem();
System.out.println(item.getItem_name());
bid.get(Bid.
[/code]

Now its all working fine, but i need to identify my Bid class using the Item_Id (which is Foreign key in BID table)
i.e
session.get(Bid.class, Item_Id)
I hope i have to make some changes at <id> tag in my bid.hbm.xml
. I am showing the bid.hbm.xml below

[code]
<hibernate-mapping>

<class name="com.hibernateproject.bean.Bid" table="jayachandra.BID" >
<id name="bidId" type="int" column="BID_ID" >
</id>
<many-to-one name="item" column="ITEM_ID" class="com.hibernateproject.bean.Item" not-null="true"/>

<property name="item1" type="java.lang.String" column="ITEM"></property>
</class>

</hibernate-mapping>
[/code]


Thanks in advance

JC


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 7:14 am 
Newbie

Joined: Fri Jan 11, 2008 5:50 am
Posts: 9
HI Jayachandra,

You're many-to-one mapping is correct, however you've got a small error with the next line, which should be mapping the BID_NAME column from the BID table to the name property in your Bid class. This gives you a bid.hbm.xml mapping file as follows:

Code:
<hibernate-mapping>

<class name="com.hibernateproject.bean.Bid" table="jayachandra.BID" >
  <id name="bidId" type="int" column="BID_ID" >
  </id>
  <many-to-one name="item" column="ITEM_ID" class="com.hibernateproject.bean.Item" not-null="true"/>
  <property name="name" type="java.lang.String" column="BID_NAME" />
</class>

</hibernate-mapping>


By default hibernate will pick up the id entity in the corresponding Item.hbm.xml mapping file.

Hope this helps

Andy


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