aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate association mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate association mapping" Watch "Hibernate association mapping" New topic
Author

Hibernate association mapping

Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi All,

I have 2 tables : Employee and EmployeeRole.
Both tables have their own Primary Keys. Employee has EmpId and EmployeeRole has EmpRoleId.

How can i make one to one mapping between these 2 tables in Hibernate.

Please guide me with both the hbm files.

Thanks in advance.

[ Edited to have a more meaningful subject line - Paul Sturrock ]
[ December 09, 2008: Message edited by: Paul Sturrock ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

Girish,

Welcome to JavaRanch

You will find a lot of information in the Hibernate reference guide. It also has a chapter for one-to-one mappings.


[My Blog] [JavaRanch Journal]
Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi JaiKiran, Thanks for your reply, but i have already read that. I think you have not seen my problem carefully. I have 2 primary Keys here. Not one PK as FK in other table. So how do i map these. My tables are : Employee : EmpId (PK) EmpName emailId EmployeeRole : EmpRoleId(PK) EmpId ( FK from Employee table) RoleId . Now can you help me.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I have 2 primary Keys here.

Not sure why this is relevant? They are primary keys for different entities. (after all, you can't have two primary keys for one entity).

Without knowing the cardinality of the relationship I can't give you an example. But the section on associations covers all the common ones (with examples).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi Paul, I dont have 2 Primary Keys for one entity. I have 2 entites : Employee and Role. And both have there own Primary Keys as EmpId and EmpRoleId respectively. Now can you tell me how to create one to one mapping in this scenario. Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Girish Kumar Prabhakar:
Hi Paul, I dont have 2 Primary Keys for one entity. I have 2 entites : Employee and Role. And both have there own Primary Keys as EmpId and EmpRoleId respectively. Now can you tell me how to create one to one mapping in this scenario. Thanks


Yes I can. This document contains examples of all common association mappings, including an example of one-to-one.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Girish,

To map between two entities you need to find out the cardinality. What is
relationship between Employee and EmployeeRole entities.

I may suppose following associations:
- One Employee can have multiple roles.
- One Employee can have exactly one role.

If two entities share the primary key you can have one-to-one relationship
in either unidirectional or bidirectional.

Unidirectional: If you are given Employee object, you can tell what role
is assigned to him. But EmployeeRole doesn't tell you which Employee does
it belong to.

BiDirectional: In both the way you can find out each other entity, as both
will have reference to each other.

As you said both the tables have primary keys EmployeeId and EmployeeRoleId.
I do not find any association building clue using both, as individual
represent their own entities.


cmbhatt
Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi Chandra,

Thanks for your reply.

Here One Employee can have exactly one Role.
I am attaching the hbm files of both.
Please have a look and guide me.

Employee.hbm.xml



EmployeeRole.hbm.xml

and i am trying to do :



I only want to update in EMployeeRole and not in Employee.

Can you suggest any thing.

Thanks

[ Edited to use code tags - Paul Sturrock ]
[ December 10, 2008: Message edited by: Paul Sturrock ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Girish,












Does it adhere what your requirement says?
Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi Chandra,

Thanks for your reply, but i have different requirement, hope you can guide me through this.

I already have a row in Employee table with empId as 1000 empName as Girish and so on. Similarly i have a row corresponding to this in EmployeeRole table with empId as 1000 and roleId as 100.

I only want to update the EmployeeRole table and set roleId as 200 for empId as 1000. There will be no update or insert in Employee table.

Now can you guide on the same.

Thanks in advance.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

I only want to update the EmployeeRole table and set roleId as 200 for empId as 1000. There will be no update or insert in Employee table.


For this you want to set property on the Employee object as


Is this a way what you want?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707






Below is auto-generated table schema:



Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Girish,

I forgot to put details.

I have put unique constraint on empId of the ROLE table as said there can be
only one role assigned to an Employee.

Id field of ROLE schema is primary key.
We can't take roleId to be primary key because same role can be assigned
to more than one Employee, so it is a separate column. (values to this
column will be redundant (not unique) as per your requirement)
Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Hi Chandra,

Thanks again for your reply. The code which you gave on the basis of that i have written mine, here it is:


Session session = HibernateConnection.currentSession();
session.beginTransaction();

Employee emp = new Employee();
emp.setEmpId(1000);
emp.setEmpName("Girish");

session.saveOrUpdate(emp);
EmpRole role = new EmpRole();
role.setEmpRoleId(1);
role.setRoleId(147);
role.setEmployee(emp);
session.saveOrUpdate(role);

session.getTransaction().commit();

HibernateConnection.closeSession();


Now this code updates the EmployeeRole table and sets the new role as required.

BUT the problem which i face now in this code is that empId for this newrole in EmployeeRole table is becoming null.

What to do now???

Also i have not set empId in EmployeeRole table as Unique constraint.
will that affect this.

Waiting for your reply.

Thanks a tonn.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Girish,


BUT the problem which i face now in this code is that empId for this newrole in EmployeeRole table is becoming null.


It should not happen because there is a mapping:


It works for me.
role.setEmp(emp); and saving or updating this role, saves the empId in
the employee role table.


Also i have not set empId in EmployeeRole table as Unique constraint.
will that affect this.


No! I just made that UniqueConstraint to suffice the need that one employee
can be assigned at most one role as your requirement says.

Hope this helps you.

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

BUT the problem which i face now in this code is that empId for this newrole in EmployeeRole table is becoming null.


Are you following the schema that I say:
id, roleId, empId, ... for EmployeeRole table?


It takes null as default value for empId in EmployeeRole table.
empId is foreign key that points to PK of Employee table of course.
It should work in this regard.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate association mapping