• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Hibernate association mapping

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Girish,












Does it adhere what your requirement says?
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






Below is auto-generated table schema:



 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic