我正在编写一个简单的HQL,以找出基于部门的薪水最高的员工。在输出中,我期待员工姓名,部门名称和最高薪水。
以下是我的实体:
Employee.java
@Entity
public class Employee implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "deptartment_id")
private Department dept;
@OneToOne
@JoinColumn(name="emp_salary")
private Salary salary;
}
Department.java
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(mappedBy="dept")
private Set<Employee> employees = new HashSet<Employee>();
}
Salary.java
@Entity
public class Salary {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private int salary;
}
如果我在HQL查询下运行:
List<Object[]> results = session.createQuery(
"select d.name, max(s.salary) "
+ "from Employee e join e.dept d join e.salary s "
+ "group by d.name").list();
我获得每个部门的最高薪水。这很好,但我在输出中也需要员工姓名。
所以,如果我将我的HQL更改为:
session.createQuery(
"select e.name, d.name, max(s.salary) "
+ "from Employee e join e.dept d join e.salary s "
+ "group by d.name, e.name").list();
此查询将返回我数据库中的所有员工。
您能否告诉我什么是正确的HQL并解释我的HQL有什么问题?
这是我保存员工记录的代码:
Employee e1 = new Employee();
e1.setName("A1");
Salary salary1 = new Salary();
salary1.setSalary(10000);
e1.setSalary(salary1);
Department dept1 = new Department();
dept1.setName("Operations");
e1.setDept(dept1);
Employee e11 = new Employee();
e11.setName("A2");
Salary salary11 = new Salary();
salary11.setSalary(12000);
e11.setSalary(salary11);
Department dept11 = new Department();
dept11.setName("Operations");
e11.setDept(dept11);
Employee e12 = new Employee();
e12.setName("A3");
Salary salary12 = new Salary();
salary12.setSalary(15000);
e12.setSalary(salary12);
Department dept12 = new Department();
dept12.setName("Operations");
e12.setDept(dept12);
Employee e2 = new Employee();
e2.setName("B1");
Salary salary2 = new Salary();
salary2.setSalary(20000);
e2.setSalary(salary2);
Department dept2 = new Department();
dept2.setName("IT");
e2.setDept(dept2);
Employee e21 = new Employee();
e21.setName("B2");
Salary salary21 = new Salary();
salary21.setSalary(22000);
e21.setSalary(salary21);
Department dept21 = new Department();
dept21.setName("IT");
e21.setDept(dept21);
Employee e22 = new Employee();
e22.setName("B3");
Salary salary22 = new Salary();
salary22.setSalary(25000);
e22.setSalary(salary22);
Department dept22 = new Department();
dept22.setName("IT");
e22.setDept(dept22);
Employee e3 = new Employee();
e3.setName("C1");
Salary salary3 = new Salary();
salary3.setSalary(30000);
e3.setSalary(salary3);
Department dept3 = new Department();
dept3.setName("Software");
e3.setDept(dept3);
Employee e31 = new Employee();
e31.setName("C2");
Salary salary31 = new Salary();
salary31.setSalary(32000);
e31.setSalary(salary31);
Department dept31 = new Department();
dept31.setName("Software");
e31.setDept(dept31);
Employee e32 = new Employee();
e32.setName("C3");
Salary salary32 = new Salary();
salary32.setSalary(35000);
e32.setSalary(salary32);
Department dept32 = new Department();
dept32.setName("Software");
e32.setDept(dept32);