Forums Register Login

Hibernate Newbie - 1 to Many Relation Tutorial

+Pie Number of slices to send: Send
Good day All,

i'm new to hibernate and i would like to get myself understand on how 1-many relationship work in hibernate, so i need all your expertise experience contribution on the guidance

i have 2 tables(mySQL)and i assume one country have multiple contact, so assume i have one(country) to many(contact) relationship betwen tables :



my contact.hbm.xml :


my question here, how to i figure out country.hbm.xml for country table ?

thanks for guidance
+Pie Number of slices to send: Send
 

"my question here, how to i figure out country.hbm.xml for country table ?"



- Didn't quite get what you meant?
+Pie Number of slices to send: Send
how to i create another hbm file for country table in order to map with contact.hbm.xml to have one - many relation?
+Pie Number of slices to send: Send
Here is how you write a simple country.hbm file.



Now that you have mentioned,
[B][/B]

- How do we go about doing the relationship mapping? You want it both the sides, or only one side? Have you tried using the <key-many-to-one>, <set>, <list>, <bag> tags in the hbm?
+Pie Number of slices to send: Send
Hi Kumar,

if we go for one side , how the mapping will look like ?
+Pie Number of slices to send: Send
I see that one contact is mapped to one country and not many.
So try <one-to-one> mapping from the contact side to the country side.
Show us your xml file after that.
+Pie Number of slices to send: Send
good day Kumar,

no doubt one contact is mapped to one country and not many, but what if one country will have many contact ? do you think is valid ?
+Pie Number of slices to send: Send
for one to one case,

i try the following :

contact.hbm.xml


country.hbm.xml


it give me error like "Attribute "column" must be declared for element type "one-to-one""
+Pie Number of slices to send: Send
Check the hibernate reference document and see if there is any attribute called column for a <one-to-one> mapping.
+Pie Number of slices to send: Send
Hi Kumar, i really cannot get it ...can you show me how would it be?

my latest try :

contact.hbm.xml


country.hbm.xml


getting more and more error

error code :



+Pie Number of slices to send: Send
This shouldn't be this difficult.

One Contact can have One Country.

One Country can have many Contacts.

sql



There really should be a foreign key relationship on your Contact table.


Contact.hbm.xml




Country.hbm.xml




Contact.java



Country.java



[ August 09, 2007: Message edited by: Nathan Hook ]

[ August 09, 2007: Message edited by: Nathan Hook ]
[ August 09, 2007: Message edited by: Nathan Hook ]
+Pie Number of slices to send: Send
Thanks Nathan Hook, it was great example as i really confuse myself on this ....let me try out later and let you know if i have any doubts, many thanks again on this
+Pie Number of slices to send: Send
Hi Nathan and kumar,

thanks for the sample, it work ..

i have some questions, below is my implementation :



the DB table result will show below :




my question here :




1)if i would like to have country_id to be string , for example country_id like "JP" , and i have modify the coutry.hbm.xml as above, it prompt me error like Table server.country' doesn't exist , is it the right way to do it ?
+Pie Number of slices to send: Send
I think there is a little bit of a disconnect on how/why to use Hibernate.

Hibernate is used to do Object-Relational Mapping. Meaning that Hibernate will take your object model and then persist that object model to your relational database.

So take the following example:



When we're setting a Country on a User we're actually setting the entire object that we are using to represent a Country on our User.

We are not setting a specific value of that Country's value on the User.

So the database tables would like the following for storing the above classes:



The above are tables that are in a specific normal form. One of the reasons for using relational databases, to reduce the amount of duplicated data.

With what you're doing by placing the name of the country in the User table, is you're lowering your normal form of your database. Which is fine, but what is the purpose of the country table? Its not even being referenced and therefore there isn't any referential integrity.

My guess is you're trying to build something called a FACT table that is easy to query. If that is your case then you will need to rethink how you're using hibernate.
+Pie Number of slices to send: Send
Say that we have created a country object and have created 2 contact objects

Then we have set each contact object reference the 1 country object
i.e.


Now save all of these objects using Hibernate save() function etc...

Now how do we reconstitute (ie what does the code look like) to get this data from the database back into Java object form, like diagram below??

[ January 20, 2008: Message edited by: Rohan Truscott ]
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 12230 times.
Similar Threads
Foreign key must have same number of columns as the referenced primary key
Hibernate 3.0 : Where am I going wrong?
Hibernate is driving me crazy !
Problem while Join in hibernate
How do I avoid duplicate inserts in YoutubeFeed table?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:14:14.