• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate : Hibernate :Mapping Multiple Table for single POJO class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Rodrigo

I also got solution.....
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if we have a single pojo class, but 2 diiferent tables in 2 different databases? is it possible? if yes can anyone explain?
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic