JPA - 与加入选择 - 如何制作两个相关的表
问题描述:
我有id_employee与relacionship的两张表。JPA - 与加入选择 - 如何制作两个相关的表
--------------------------- --------------------------- table employee table timesheet --------------------------- --------------------------- id_employee id_time name_employee date_entry --------------------------- quant_hour id_employee ---------------------------
我需要做一个选择,返回雇员表中的所有记录和现有相关表格时间表中的小时数。某些员工在时间表中没有记录小时数,但应该出现在列表中,因为他们已在员工表中注册。
什么JPQL查询这个?
类Employee
@Entity
public class Employee {
@Id @GeneratedValue
private Long id_employee;
private String name_employee ;
//GETS and SETS
}
类的TimeSheet
@Entity
public class Timesheet {
@Id @GeneratedValue
private Long id_time;
private Double quant_hour;
private Date date_entry;
@ManyToOne
private Employee employee;
//GETS and SETS
}
答
只要两个表被正确映射,Employee实体将具有类型时间表实体的属性(或集合),因此,只需要获得员工名单:
SELECT e FROM Employee e
JPA自动当你想从Employee实体读取Timesheet(或时间表列表)时会检索它。
由于JPQL在entites上操作,因此它取决于您的实体而不是您的表。请发布雇员和时间表班级代码(只有属性和注释,而不是gettes/setters) – kostja 2013-05-02 15:53:54
更新请求...包括类! – 2013-05-02 16:53:54