Hello!
I’m new to JPA so maybe the question for someone will seem trivial, but I’m still not clear about the creation of the tables by the provider, for example, in this example it’s created in the table using @Inheritance(strategy = InheritanceType.SINGLE_TABLE) following columns: employee_id which is fk on id , Id himself as Primary example on the screen key of the golden color, manager_id as fk for id ..
Why does the hibernate provider create fk on id from these columns?
The employee_id column that was created by the provider does not belong to the fields of my classes, why is this column being created?
What kind of ID as Primary is the golden key color, is the id from the Employee class not Primary?
An example of my code:
Employee:
@Entity @Table (name = "employees") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "test" , discriminatorType = DiscriminatorType.STRING) public class Employee { public Employee(int id, String name, String lastname, double salary, String spec) { this.id = id; this.name = name; this.salary = salary; this.specialization = spec; this.lastname = lastname; } public int getId() { return id; } public String getName() { return name; } public double getSalary() { return salary; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", salary=" + salary + ", spec='" + specialization + '\'' + '}'; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } @Id @Column(name = "id") private int id; @Column(name = "name") private String name; @Column(name = "lastname") private String lastname; @Column(name = "salary") private double salary; @Column (name = "specialization") private String specialization; public Manager getManager() { return manager; } public void setManager(Manager manager) { this.manager = manager; } @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE}, fetch = FetchType.EAGER , mappedBy = "employee") private Manager manager; public Employee(){} } Manager:
@Entity @DiscriminatorValue("M") public class Manager extends Employee{ public Manager(){} public Set<Departments> getDepartment() { return department; } public void setDepartment(Set<Departments> department) { this.department = department; } public String getManager_country() { return manager_country; } public void setManager_country(String manager_country) { this.manager_country = manager_country; } public String getManager_address() { return manager_address; } public void setManager_address(String manager_address) { this.manager_address = manager_address; } public String getManager_experience() { return manager_experience; } public void setManager_experience(String manager_experience) { this.manager_experience = manager_experience; } @Override public String toString() { return "Manager{" + "department=" + department + ", manager_address='" + manager_address + '\'' + ", manager_experience='" + manager_experience + '\'' + ", manager_country='" + manager_country + '\'' + ", id=" + id + '}'; } @OneToMany(fetch = FetchType.EAGER , cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE} , mappedBy = "manager") private Set<Departments> department; @OneToOne private Employee employee; public Manager(String manager_experience,String manager_address, String manager_country) { this.manager_experience = manager_experience; this.manager_address = manager_address; this.manager_country = manager_country; } @Column(name = "experience") private String manager_experience; @Column(name = "address") private String manager_address; @Column(name = "country") private String manager_country; @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "manager_id" ,insertable = false , updatable = false) private int id; } Departments:
@Entity @DiscriminatorValue("D") public class Departments extends Manager{ public String getSales() { return sales; } public void setSales(String sales) { this.sales = sales; } public String getCostumer_support() { return costumer_support; } public void setCostumer_support(String costumer_support) { this.costumer_support = costumer_support; } public String getDevelopers() { return developers; } public void setDevelopers(String developers) { this.developers = developers; } public String getSystem_administrators() { return system_administrators; } public void setSystem_administrators(String system_administrators) { this.system_administrators = system_administrators; } public String getAccounting_department() { return accounting_department; } public void setAccounting_department(String accounting_department) { this.accounting_department = accounting_department; } @ManyToOne private Manager manager; @Column(name = "costumer_support_department" , unique = false, updatable = true , insertable = true , nullable = true) private String costumer_support; @Column(name = "developers_department" , unique = false, updatable = true , insertable = true , nullable = true) private String developers; @Column(name = "sys_admins_department" , unique = false, updatable = true , insertable = true , nullable = true) private String system_administrators; @Column(name = "accounting_department" , unique = false, updatable = true , insertable = true , nullable = true) private String accounting_department; @Column(name = "sales_department" , unique = false, updatable = true , insertable = true , nullable = true) private String sales; public Departments(){} } 