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