cha*_*dra 6 hibernate self-referencing-table
我被问到如何为表中引用表的主键的列创建hibernate映射.
例如,Employee表将EMP_ID作为主键,并且还具有MGR_ID列以了解员工的经理.由于经理也是员工,它将在同一个表中.因此,每个Employee行都有一个经理Id,它也是一个雇员.
请帮助我解决这种情况.谢谢.
Cha*_*nya 13
你可以manager在Employee课堂上引用它.
该实体看起来像这样:
@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<employee>();
public Employee() {
}
public Employee(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
// Getter and Setter methods
}
Run Code Online (Sandbox Code Playgroud)
有关完整示例,请参阅此链接:
Hibernate Self Join Annotations一对多映射示例
| 归档时间: |
|
| 查看次数: |
5796 次 |
| 最近记录: |