File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - how to compose MANAGER-EMPLOYEE association?   (newbie) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate - how to compose MANAGER-EMPLOYEE association?   (newbie) " Watch "Hibernate - how to compose MANAGER-EMPLOYEE association?   (newbie) " New topic
Author

Hibernate - how to compose MANAGER-EMPLOYEE association? (newbie)

Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
Hi,

Using Hibernate I wonder how can I solve this issue: I have a table of USER, some are employees some are managers. Managers have a NULL value in the MANAGER_ID (I over simplify this issue on purpose).

Q: what should be the relationship in the POJO for USER so when I select a USER with NULL value - I will be able to get a list of all employees
AND when selecting an employee - I can get the Manager.

A MANAGER_ID is a USER_ID (see example below)



Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4962

I think you may have lurked into the muddy waters of Hibernate Inheritance Mapping.

How to map Inheritance with Hibernate and JPA: Tutorial

You have an 'is-a' as opposed to a 'has-a' relationship there. So, I think you'll need inheritance.

-Cameron

Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
I think your entity should have a field name manager of type User which should be nullable and it should have one to one association with the User entity and ofcourse your manager field will NOT be unique.(Such type of Joins are called Self Joins in SQL)


Rahul Babbar
Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75


solved!
Thank you guys.
 
 
subject: Hibernate - how to compose MANAGER-EMPLOYEE association? (newbie)
 
Threads others viewed
Simple requirement .. but I don't know how!
Basic JPA @OneToMany / @ManyToOne mapping trouble (related to hibernate?)
Edit primary key, if it's a foreign key
how to update OneToMany
Outer join between 3 entities in HQL
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com