2013-12-19 3 views
0

내 서버 측 구성 요소는 JPA와 함께 EJB입니다. 엔티티HQL 쿼리가 예상대로 작동하지 않습니다.

@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; 
} 

} 

내 테이블이

직원

과 같은 임베디드

@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; 
} 

} 

을 따를

내 법인 보인다

========

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 | 
============================= 

조회 수 그러나 내가 받고 있어요는

emp_id | name | designation | 
==============|============== 
1001 | xxxx | programmer | 
1001 | xxxx | programmer | 6 rows 
1001 | xxxx | programmer | 
1001 | xxxx | programmer | 
1001 | xxxx | programmer | 
1001 | xxxx | programmer | 
============================= 

이 왜 중복 값 만 행의 정확한 수를주고있다 JPQL 출력을 통해 무엇입니까? 문제는 어떻게 해결할 수 있습니까?

+0

http://en.wikibooks.org/wiki/Java_Persistence/JPQL#Select_query_examples 여기에 어떤 문제를보고하지 않습니다. 루프 내부에서 첫 번째 결과를 계속해서 인쇄하지 않도록하십시오. –

+1

그것은 JPQL이 아닌 HQL입니다. 차이점이 있습니다. 그리고 HQL을 사용하는 경우 JPQL이 아니기 때문에 질문을 Hibernate로 태그를 지정하십시오. – DataNucleus

답변

1

이전에 키워드없이없이 으로 작성된 JPQL 선택문을 보지 못했습니다. 나는 그것이 잘못된 것이 결정적으로 말할 수는 없지만 다음과 같이 쿼리를 재구성하려고 할 수 있습니다

String query = "select e from Employee e where e.designation = :design "; 

가 나는 또한 클래스 이름 "직원"으로 테이블 이름 "직원"대체 좋을 것 (위 그림과 같이). 다시 말하지만, 내가하는 일이 잘못되었다는 사실을 알지는 못한다.하지만 나는 항상 JPQL의 테이블 이름보다는 (규정되지 않은) Java 클래스 이름을 사용했다.

는 여기에 몇 가지 더 예 :

관련 문제