Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Join using Hibernate

 
Mahek Singhal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I m trying to join two tables say hiber_employee(id,empId,name,designation) and hiber_company(companyid,empid,grade,salary).
but i dont want to store the joined values into a new table.
i have made three persistence classes for each table and 1 POJO for the join.
my hiber_join.hbm.xml file looks like..

<hibernate-mapping >
<class name="join.Hiber_Join" >
<id name="joinId" type="int" unsaved-value="null">
<column name="ID" sql-type="number" not-null="true"/>
<generator class="hilo"/>
</id>
<property name="empId"/>
<property name="name"/>
<property name="grade"/>
<property name="salary"/>
<property name="designation"/>
</class>
<class name="join.Hiber_Company" table="hiber_company">
<id name="companyId" type="int" unsaved-value="null">
<column name="COMPANY_ID" sql-type="number" not-null="true"/>
<generator class="hilo"/>
</id>
<property name="empId"/>
<property name="grade"/>
<property name="salary"/>
</class>
<class name="join.Hiber_Employee" table="hiber_employee">
<id name="id" type="int" unsaved-value="null">
<column name="ID" sql-type="number" not-null="true"/>
<generator class="hilo"/>
</id>
<property name="name"/>
<property name="empId"/>
<property name="desgn"/>
</class>
</hibernate-mapping>

is this the right way of writing .hbm.xml in case of join...??
 
Jan Vandernoot
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

You don't do joins in Hibernate, Hibernate does it for you. You should read the Hibernate reference manual (http://www.hibernate.org/hib_docs/v3/reference/en/html/), it explains everything you need to know...

Regards,
Jan
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're trying to design your classes around the database. What you should be doing is design the classes then map them to the database. It looks to me like you should only have two classes, somthing like:

From there you could build your Company.hbm.xml and Employee.hbm.xml
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic