有人问我,如何为表中引用表主键的列创建HibernateMap。例如,Employee表具有EMP_ID作为主键,并且还具有MGR_ID列以了解该雇员的经理。由于经理也是Employee,因此它将位于同一表中。因此,每个Employee行都具有经理ID,该经理ID也是雇员。1.如何为这个Employee类创建HibernateMap?
gajydyqb1#
您可以在Employee类中引用manager。实体如下所示:
Employee
manager
@Entity @Table(name="EMPLOYEE") public class Employee { @Id @Column(name="EMPLOYEE_ID") @GeneratedValue private Long employeeId; @Column(name="FIRSTNAME") private String firstname; @Column(name="LASTNAME") private String lastname; @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="manager_id") private Employee manager; @OneToMany(mappedBy="manager") private Set<Employee> subordinates = new HashSet<>(); public Employee() { } public Employee(String firstname, String lastname) { this.firstname = firstname; this.lastname = lastname; } // Getter and Setter methods }
有关完整示例,请参阅此链接:Hibernate Self Join Annotations One To Many mapping example
1条答案
按热度按时间gajydyqb1#
您可以在
Employee
类中引用manager
。实体如下所示:
有关完整示例,请参阅此链接:
Hibernate Self Join Annotations One To Many mapping example