This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


solved!
Thank you guys.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!