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 ?
- 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?
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.
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
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 ?
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.