我试图找出以下是否可能,使用javax persistance JPA:使用别名连接相同的表两次,以便在select语句中返回两个不同的值。对于下面的示例SQL,这两个值将是boss.name和employee.name,它们都存储在同一列的同一个表中。
SELECT staff.id, boss.name, employee.name
FROM staff, person as 'boss', person as 'employee',
WHERE
staff.id = boss.staff_id
AND staff.id = employee.staff_id
AND boss.code = 'boss'
AND employee.code = 'employee'
另外,如果可能的话,我想用直接的JDK而不是Hibernate来做这件事。谢谢!
答案 0 :(得分:1)
我认为你只需要与Staff的Person实体建立两种关系,例如:
@Entity
public class Staff {
...
@OneToOne
@JoinColumn(name = "boss_id")
Person boss;
@OneToOne
@JoinColumn(name = "employee_id")
Person employee;
...
}