-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Where is the parent/child relationship stored in Hibernate
PostPosted: Fri Nov 22, 2013 1:20 am 
Newbie

Joined: Wed Nov 20, 2013 4:16 am
Posts: 4
Hibernate one-to-many example's mapping file:
Code:
<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail.
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <set name="certificates" cascade="all">
         <key column="employee_id"/>
         <one-to-many class="Certificate"/>
      </set>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>

   <class name="Certificate" table="CERTIFICATE">
      <meta attribute="class-description">
         This class contains the certificate records.
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="name" column="certificate_name" type="string"/>
   </class>

</hibernate-mapping>

The program runs smoothly but I am new to Hibernate, I just want to understand that employee information is stored in table EMPLOYEE, certificate's information is stored in table CERTIFICATE, now because one employee can have many certificates we used one-to-many mapping i.e. by adding this to the mapping file:

Code:
  <set name="certificates" cascade="all">
     <key column="employee_id"/>
     <one-to-many class="Certificate"/>
  </set>

But now what I want to understand is that when we fetch a record from table EMPLOYEE how am I able to access the certificates of that employee, what I don't understand is that my table EMPLOYEE has only four columns 'id', 'firstname', 'lastname' and 'salary' so how come when I fetch a record the object comes with certifications as well.

In a many to many situation, typically a linking(intermediate) table is used. This allows SQL or whatever database to maintain the relationships and hibernate simply relies on the mapping to pull the right stuff out of the database. But how does it manage with one-to-many mapping


Top
 Profile  
 
 Post subject: Re: Where is the parent/child relationship stored in Hibernate
PostPosted: Fri Nov 22, 2013 2:05 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
There will be a column named "employee_id" in the CERTIFICATE table. (Which references the id-column of your EMPLOYEE table and is enforce by a foreign key constraint.) Give it a try...


Top
 Profile  
 
 Post subject: Re: Where is the parent/child relationship stored in Hibernate
PostPosted: Fri Nov 22, 2013 2:11 am 
Newbie

Joined: Wed Nov 20, 2013 4:16 am
Posts: 4
Oh so even when I do not define that column it automatically gets created. Nice. Thanks a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.