Hibernate: Parent/Child relationship in a single-table

前端 未结 3 771
温柔的废话
温柔的废话 2021-02-15 15:08

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

3条回答
  •  余生分开走
    2021-02-15 15:19

    Thanks a ton guys. I created my Employee Entity as follows:

    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(
        name="EMPLOYEE_TYPE", 
        discriminatorType = DiscriminatorType.STRING
    )
    @DiscriminatorValue("Employee")
    public abstract class Employee {
    
        @Id 
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="EMPLOYEE_ID") 
        private Integer empId = null;
    
        @Column(name="EMPLOYEE_NAME") 
        private String empName = null;
    
        @Column(name="EMPLOYEE_SECRETARY")
        private String secretary;
    
        @Column(name="EMPLOYEE_PERKS")
        private int perks;       
    
        @ManyToOne(targetEntity = Employee.class, optional=true)
    @JoinColumn(name="MANAGER_ID", nullable=true)
    private Employee manager = null;
    
        @OneToMany  
        private Set reportees = new HashSet();
    
        ...        
    
        public Set getReportees() {
            return reportees;
        } 
    }
    

    I then added other Entity classes with no body but just Discriminator columns values, such as Manager, CEO and AsstManager. I chose to let Hibernate create the table for me. Following is the main program:

    SessionFactory sessionFactory = HibernateUtil.sessionFactory;
    Session session = sessionFactory.openSession();
    Transaction newTrans = session.beginTransaction();
    
    CEO empCeo = new CEO();
    empCeo.setEmpName("Mr CEO");
    empCeo.setSecretary("Ms Lily");
    
    Manager empMgr = new Manager();
    empMgr.setEmpName("Mr Manager1");
    empMgr.setPerks(1000);
    empMgr.setManager(empCeo);
    
    Manager empMgr1 = new Manager();
    empMgr1.setEmpName("Mr Manager2");
    empMgr1.setPerks(2000);
    empMgr1.setManager(empCeo);
    
    AsstManager asstMgr = new AsstManager();
    asstMgr.setEmpName("Mr Asst Manager");
    asstMgr.setManager(empMgr);
    
    session.save(empCeo);
    session.save(empMgr);
    session.save(empMgr1);
    session.save(asstMgr);
    newTrans.commit();
    
    System.out.println("Mr Manager1's manager is : "
            + empMgr.getManager().getEmpName());
    System.out.println("CEO's manager is : " + empCeo.getManager());
    System.out.println("Asst Manager's manager is : " + asstMgr.getManager());
    System.out.println("Persons Reporting to CEO: " + empCeo.getReportees());
    
    session.clear();
    session.close();
    

    The code runs fine, Hibernate was creating a column "MANAGER_EMPLOYEE_ID" on its own where it stores the FK. I specified the JoinColumn name to make it "MANAGER_ID". Hibernate also creates a table EMPLOYEE_EMPLOYED, however the data is not being persisted there.

    Method getReportees() method returns a null, while getManager() works fine, as expected.

提交回复
热议问题