Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes Mapping same POJO to more than one table in Hibernate XML mapping files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping same POJO to more than one table in Hibernate XML mapping files" Watch "Mapping same POJO to more than one table in Hibernate XML mapping files" New topic
Author

Mapping same POJO to more than one table in Hibernate XML mapping files

harsh anajwala
Greenhorn

Joined: Oct 13, 2009
Posts: 16
Hello,

I have back up table kind of thing in my database. i.e after some period some table are created newly(due to huge data present in the existing table) which is same as existing table. This new table will be used to insert new data but old table can be useful for select purpose.

So i would like to map single POJO to both table. These tables are created dynamically after particular period so at the time of generation of new table ,this table should be mapped to same POJO to which existing table was mapped...


So can i mention two tables in class tag for the mapping?
Or any other way to do this...


Thank you
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
It is possible to map a single POJO class to two tables using Hibernate. First create your object and its related HBM (xml) file. Then copy the HBM file and rename it to something similar. In the first HBM you will have something like the following. Please note the use of entity-name element.


In the second HBM you will have something like:


Then in your data layer you will have code something like:


Of course the above requires that your table actually exist, thus the dynamic creation of the table will be a problem.

Good luck friend,


Ran
harsh anajwala
Greenhorn

Joined: Oct 13, 2009
Posts: 16
Thank you Ran for the reply...I got it..But do i need to make two different XML for both mapping purpose.

And if I merge both XML in to one XML File then should I simply copy mapping(which is same) of both files one after another?that will create redundancy..so ideally that should not be the case..
Can you explain me How merged XML will look like?

Thank You..
Shirish Bathe
Greenhorn

Joined: Dec 01, 2012
Posts: 1
Hi Harsh/Ran

How to retrieve records for either Table1 or Table2. As similer to saveOrUpdate API, Query.list does not parameter to specify entity-name

Thanks,
Shirish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping same POJO to more than one table in Hibernate XML mapping files