This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Newbie - 1 to Many Relation Tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Newbie - 1 to Many Relation Tutorial" Watch "Hibernate Newbie - 1 to Many Relation Tutorial" New topic
Author

Hibernate Newbie - 1 to Many Relation Tutorial

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

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


- Didn't quite get what you meant?


If you are not laughing at yourself, then you just didn't get the joke.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
how to i create another hbm file for country table in order to map with contact.hbm.xml to have one - many relation?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

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?
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
Hi Kumar,

if we go for one side , how the mapping will look like ?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

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.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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 ?
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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""
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Check the hibernate reference document and see if there is any attribute called column for a <one-to-one> mapping.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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 :



Nathan Hook
Ranch Hand

Joined: Jan 10, 2005
Posts: 81
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 ]
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 438
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 ?
Nathan Hook
Ranch Hand

Joined: Jan 10, 2005
Posts: 81
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.
Rohan Truscott
Greenhorn

Joined: Jan 20, 2008
Posts: 1
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Newbie - 1 to Many Relation Tutorial
 
Similar Threads
Problem while Join in hibernate
Hibernate 3.0 : Where am I going wrong?
How do I avoid duplicate inserts in YoutubeFeed table?
Hibernate is driving me crazy !
Foreign key must have same number of columns as the referenced primary key