reisito wrote:
Hi All
I am very new to hibernate and little confused about primary key and foreignkey in .hbm.xml file.
Lets say I have a table called , User which has following fields
ID (Primary key)
User name
Password
I have another table called User_Details
User_Details
ID (PK+Foreign key to user table)
First Name
Last Name
Address
City
Country
Now I want to creater .hbm.xml files for both the tables how do I do that?
I dont have any tool which will generate these files automatically, so I have to really do it by myself.
Please advise how do I achieve this?
With your original example above, you might want to look at the example at
http://www.hibernate.org/41.html which talks about using a lightweight class.
However with the Book/Sales example, you can specify something like this:
Code:
<hibernate-mapping">
<class name="Book" table="BOOK">
<id column="ID" name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property column="TITLE" name="bookTitle" type="java.lang.Long"/>
.... additional properties....
<one-to-one name="sale" class="Sale" cascade="...."/>
</class>
<class name="Sale" table="SALE">
<id column="ID" name="id" type="java.lang.Long">
<generator class="foreign">
<param name="property">book</param>
</generator>
</id>
<one-to-one name="book" class="Book" constrained="true"/>
<property column="SALE_DATE" name="saleDate" type="java.util.Date"/>
.... additional properties....
</class>
</hibernate-mapping>
The foriegn ID generator will save the ID of the Sale using the generated ID provided by the book.