Note that there are some explanatory texts on larger screens.

plurals
  1. PORegarding one to one mapping in hibernate
    text
    copied!<p>I am a new bie to the world of hibernate could you please advise , I was going through one to one relationship in Hibernate, As per my analysis , one to one hibernate relationships can be established by three ways..</p> <pre><code>1)Through Join concept 2)Same primary key in both the tables 3)Primary key and foriegn key relationship in both the tables </code></pre> <p><strong>Please advise , the above three ways to achieve the one to one mapping is correct or I am missing something then please advise, and also please advise that the below hbm mapping files that I am using are correct one.if not then please advise</strong>.</p> <p>1) Through Join concept :-<br> One way to achieve to one to one relationship is by joining concept , following xml is being used for that</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="mypack.Person" table="person21"&gt; &lt;id name="personId" type="int"&gt; &lt;generator class="increment"&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property name="name"/&gt; &lt;join table="personAddress"&gt; &lt;key column="personId"/&gt; &lt;many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/&gt; &lt;/join&gt; &lt;/class&gt; &lt;class name="mypack.Address" table="address21"&gt; &lt;id name="id" column="addressId" type="int"&gt; &lt;generator class="increment"/&gt; &lt;/id&gt; &lt;property name="city"/&gt; &lt;property name="state"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>2) Same primary key in both the tables :-</p> <p>Using same primary key in both the tables , following hbm is being used for that</p> <pre><code> &lt;class name="mypack.Address" table="address31"&gt; &lt;id name="id" column="addressId" type="int"&gt; &lt;generator class="increment"/&gt; &lt;/id&gt; &lt;property name="city"/&gt; &lt;property name="state"/&gt; &lt;/class&gt; &lt;class name="mypack.Person" table="person31"&gt; &lt;id name="personId" type="int"&gt; &lt;generator class="foreign"&gt; &lt;param name="property"&gt;address&lt;/param&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property name="name"/&gt; &lt;one-to-one name="address" class="mypack.Address"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>3)Primary key and foriegn key relationship in both the tables :-</p> <p>Primary key in one table and foriegn key in another table. below is the hbm for this</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="mypack.Person"&gt; &lt;id name="personId" type="int"&gt; &lt;generator class="increment"/&gt; &lt;/id&gt; &lt;property name="name"/&gt; &lt;many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/&gt; &lt;/class&gt; &lt;class name="mypack.Address"&gt; &lt;id name="id" column="addressId" type="int"&gt; &lt;generator class="increment"/&gt; &lt;/id&gt; &lt;property name="city"/&gt; &lt;property name="state"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>folks please advise. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload