Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate :Mapping Multiple Table for single POJO class(with my problem explanation )

 
Arun G Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to translate an SQL query to hibernate. But the problem is with the mapping. I have a class named UserBean to which values from different table is required. So I don�t know how to map this UserBean class, as there is no table in my data base that corresponds to UserBean. The properties in UserBean needs to get values from three tables, named

1. seat_user_alloted

allot_Id (primary,autoincrement),
seat_Id,
user_Id,
active_Date,
deactive_Date,
active_Status,
passwd

2. user_setup

user_Id(primary,autoincrement),
employee_Id,
user_Name,
active_Date,
deactive_Date,
active_Status,
user_Code

3. hierarchy_setup

hierarchy_Id (primary,autoincrement),
hierarchy_Name,
hierarchy_Code,
hierarchy_Level,
parent_hierarchy,
hierarchy_Tree,
active_Date,
deactive_Date,
active_Status,
category,
allot_Status



The query that I need to translate is shown below

select
seat_user_alloted.seat_Id as seatId,
seat_user_alloted.user_Id as userId,
hierarchy_setup.hierarchy_Name as seatName,
hierarchy_setup.hierarchy_Code as seatCode,
user_setup.user_Code as empCode,
user_setup.user_Name as empName
from
user_setup,hierarchy_setup,seat_user_alloted
where
seat_user_alloted.user_Id=user_setup.user_Id
and
seat_user_alloted.seat_Id=hierarchy_setup.hierarchy_Id
and
user_setup.active_Status='Y'
and
hierarchy_setup.active_Status='Y'
and
seat_user_alloted.active_Status='Y'


note: In the select query above� that table-column-values are got in to the UserBean which contains the properties given below with getter and setters.

UserBean
private String seatId;
private String userId;
private String seatName;
private String seatCode;
private String empCode;
private String empName;
private String userOffice;
private String userManager;
private String userSection;

Now how can I map this user bean to three tables� or please some one post a solution.

[ November 19, 2008: Message edited by: Arun G Nair ]
[ November 19, 2008: Message edited by: Arun G Nair ]
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

I think single pojo solution is not possible here.
Hope , following solution will help i your case.

Create pojo for each table like for user_setup create pojo having all column in that table,
same for hierarchy_setup ,
And while creating pojo for seat_user_alloted add object of user_setup pojo
and hierarchy_setup pojo.

So your seat_user_alloted pojo will look like :



Thanks
- Kuldeep
 
Arun G Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thanks for your suggestion Kuldeep!!! But doing so.. how shall I get values, and to which object I will get that... Actually in ibatis this is not an issue, because we can directly assign the values from different tables to a single class.

But I think, working in hibernate also, this kind of issue will come again and again... so could you please explain the solution in detail, if possible..
 
Arun G Nair
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please show me how the "hbm.xml" file for my case will look like...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic