• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate : Hibernate :Mapping Multiple Table for single POJO class

 
sachin Misurkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have requirement to map Different Table to one POJO class.
Example;
Table
1)Employee:
-EmpName
-EmpAddress
-EmpPhone

2)HealthDetails :
- Height
- Weight
- BloodGroup

I want create single POJO class which contains fields from Both table.
Pleasez suggest me How to do hibernate mapping(Mapping *.hbm.xml file) for such
scenario....

Thanks in Advance
regards
sachin misurkar
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this

You must have something like:
 
sachin Misurkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Rodrigo

I also got solution.....
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just as easy with annotations:

Mapping One Class to Two Tables: The Secondary Table!

One Class to Two Tables

From time to time, you'll run into a scenario where your application defines a single class, but the database maintains the correspoinding information in two separate tables. Well, with Hibernate, that's not a problem. All you have to do to map one class to two database tables is to define the first table mapping as you normally would, and then specify the name of the second table your class uses with the very intelligently named @SecondaryTable annotation. From there, when you get to a field that is supposed to be stored in the second table, well, you just mention the second table's name in the @Column annotation. It's all very simple and straight forward.


Here's a little scenario I map in a tutorial of mine. You'll notice that the code is pretty clean.





Mapping Tutorial

So there you go! It's as easy as using the SecondaryTable annotation!

-Cameron McKenzie
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if these 2 tables doesn't have same composite keys...
In this situation how can i map one class to these 2 tables ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic