0
我的服务器端组件是EJB和JPA。HQL查询不能按预期工作
我的实体看起来遵循
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 4235645698986231545L;
@EmbeddedId
private EmployeeId id;
@Column
private String designation;
public Employee() {
}
public EmployeeId getId() {
return this.id;
}
public void setId(EmployeeId id) {
this.id = id;
}
public String getDesignation() {
return this.designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}
嵌入式实体
@Embeddable
public class EmployeeId implements java.io.Serializable {
private static final long serialVersionUID = 4542369821217566566L;
@Column(name = "emp_id")
private Integer empId;
@Size(max = 10)
private String name;
public EmployeeId() {
}
public Byte getEmpId() {
return this.empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getName() {
return this.stationId;
}
public void setName(String name) {
this.name = name;
}
}
我的表看起来像这样
员工
========
emp_id | name | designation |
==============|==============
1001 | xxxx | programmer |
1001 | yyyy | programmer |
1002 | zzzz | tester |
1003 | aaaa | HR |
1004 | bbbb | Admin |
1005 | cccc | Manager |
1006 | dddd | programmer |
1007 | eeee | programmer |
1008 | ffff | programmer |
1008 | gggg | programmer |
=============================
我的JPQL查询
String query = "from Employee where designation = :design ";
public List<Employee> find(String designation) {
return getEntityManager().createQuery(query).setParameter("design", designation).getResultList();
}
我预期的结果如下。我得到这个,而手动查询
emp_id | name | designation |
==============|==============
1001 | xxxx | programmer |
1001 | yyyy | programmer |
1006 | dddd | programmer | 6 rows
1007 | eeee | programmer |
1008 | ffff | programmer |
1008 | gggg | programmer |
=============================
。但我得到的是通过JPQL输出
emp_id | name | designation |
==============|==============
1001 | xxxx | programmer |
1001 | xxxx | programmer | 6 rows
1001 | xxxx | programmer |
1001 | xxxx | programmer |
1001 | xxxx | programmer |
1001 | xxxx | programmer |
=============================
为什么给人重复的值,但行是否正确?有什么问题我该如何解决这个问题?
没有看到任何的问题在这里。确保你没有在循环内反复打印第一个结果。 –
这就是HQL而不是JPQL;有一个区别。如果使用HQL,那么将您的问题标记为Hibernate,因为它不是JPQL。 – DataNucleus