-->
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: Need help in Mapping.
PostPosted: Sat Mar 06, 2004 11:44 pm 
Newbie

Joined: Wed Feb 25, 2004 10:25 am
Posts: 1
Hi, I am new to hibernate and I have studied online materials on Hibernate. But I havent understood how to write a mapping file for the following scenaria. Please help...

I have two classes Country and State...

Country (int countryID, String countryName, String description);
State (int stateID, String stateName, Country country, String description);

And the tables are:

COUNTRY:
Integer COUNTRY_ID --> PK
Varchar COUNTRY_NAME
Varchar DESCRIPTION

STATE:
Integer STATE_ID --> PK
Varchar STATE_NAME
Integer STATE_COUNTRY_ID (FK to country's COUNTRY_ID)
Varchar DECSRIPTION


I am able to persist Country without any problem as it is quite straight forward without any foreign key relations. But I haven't figured a way to write a mapping file for State that has Country object in it.

Please provide me mapping file for State.class

Thanks
Sanskruti.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 1:46 am 
Newbie

Joined: Wed Feb 11, 2004 6:30 pm
Posts: 6
Hi Sanskruti,

Use many-to-one for country attribute of State.

class = "Country"
column="STATE_COUNTRY_ID" (column in state refering to country table)
(pls change the generator class value as per your application.)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="State" table="STATE">
<id name="stateID" column="STATE_ID" type="integer">
<generator class="identity"></generator>
</id>
<many-to-one name="country" class="Country" column="STATE_COUNTRY_ID"/>
<property name="description" column="DESCRIPTION"/>
<property name="stateName" column="STATE_NAME"/>
</class>

</hibernate-mapping>


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.