Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:42:24+00:00 2026-05-13T22:42:24+00:00

I hardly see any pointer on the following problem related to Hibernate. This pertains

  • 0

I hardly see any pointer on the following problem related to Hibernate. This pertains to implementing inheritance using a single database table with a parent-child
relationship to itself. For example:

CREATE TABLE Employee (
  empId BIGINT NOT NULL AUTO_INCREMENT,
  empName VARCHAR(100) NOT NULL,
  managerId BIGINT,
  CONSTRAINT pk_employee PRIMARY KEY (empId)
)

Here, the managerId column may be null, or may point to another row of the Employee table. Business rule requires the Employee to know about all his reportees and for him to know about his/her manager. The business rules also allow rows to have null managerId (the CEO of the organisation doesn’t have a manager).

How do we map this relationship in Hibernate, standard many-to-one relationship doesn’t work here? Especially, if I want to implement my Entities not only as a corresponding “Employee” Entity class, but rather multiple classes, such as “Manager”, “Assistant Manager”, “Engineer” etc, each inheriting from “Employee” super entity class, some entity having attributes that don’t actually apply to all, for example “Manager” gets Perks, others don’t (the corresponding table column would accept null of course).

Example code would be appreciated (I intend to use Hibernate 3 annotations).

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:42:24+00:00Added an answer on May 13, 2026 at 10:42 pm

    You are expressing two concepts here:

    1. inheritance and you want to map your inheritance hierarchy in a single table.
    2. a parent/child relationship.

    To implement 1., you’ll need to use Hibernate’s single table per class hierarchy strategy:

    @Entity
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(
        name="emptype",
        discriminatorType=DiscriminatorType.STRING
    )
    public abstract class Employee { ... }
    
    @Entity
    @DiscriminatorValue("MGR")
    public class Manager extends Employee { ... }
    

    To implement 2., you’ll need to add two self-referencing associations on Employee:

    • many employee have zero or one manager (which is also an Employee)
    • one employee has zero or many reportee(s)

    The resulting Employee may looks like this:

    @Entity
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(
        name="emptype",
        discriminatorType=DiscriminatorType.STRING
    )
    public abstract class Employee { 
    
        ... 
    
        private Employee manager;
        private Set<Employee> reportees = new HashSet<Employee>();
    
        @ManyToOne(optional = true)
        public Employee getManager() {
            return manager;
        }
    
        @OneToMany
        public Set<Employee> getReportees() {
            return reportees;
        }
    
        ...
    }
    

    And this would result in the following tables:

    CREATE TABLE EMPLOYEE_EMPLOYEE (
            EMPLOYEE_ID BIGINT NOT NULL,
            REPORTEES_ID BIGINT NOT NULL
        );
    
    CREATE TABLE EMPLOYEE (
            EMPTYPE VARCHAR(31) NOT NULL,
            ID BIGINT NOT NULL,
            NAME VARCHAR(255),
            MANAGER_ID BIGINT
        );
    
    ALTER TABLE EMPLOYEE ADD CONSTRAINT SQL100311183749050 PRIMARY KEY (ID);
    
    ALTER TABLE EMPLOYEE_EMPLOYEE ADD CONSTRAINT SQL100311183356150 PRIMARY KEY (EMPLOYEE_ID, REPORTEES_ID);
    
    ALTER TABLE EMPLOYEE ADD CONSTRAINT FK4AFD4ACE7887BF92 FOREIGN KEY (MANAGER_ID)
        REFERENCES EMPLOYEE (ID);
    
    ALTER TABLE EMPLOYEE_EMPLOYEE ADD CONSTRAINT FKDFD1791F25AA2BE0 FOREIGN KEY (REPORTEES_ID)
        REFERENCES EMPLOYEE (ID);
    
    ALTER TABLE EMPLOYEE_EMPLOYEE ADD CONSTRAINT FKDFD1791F1A4AFCF1 FOREIGN KEY (EMPLOYEE_ID)
        REFERENCES EMPLOYEE (ID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is so much written about unit testing but I have hardly found any
I am new on iphone development and I have problem with CoreData. I hardly
Should this be represented in the database as 1 table or 3 tables? I
I am reposting this question due to inability to solve the problem (original here
I hope I am using the right terminology. I have made a single-chained list.
I hardly ever see the second one used and I wonder why? Neither would
One of our users received the following unexpected error yesterday whilst using our Silverlight
I'm now blocked by this problem the entire day, read thousands of google results,
I hardly know any Jquery or Javascript. So, I beg your pardon in advance
The following code illustrates a pattern I sometimes see, whereby an object is transformed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.