2014-01-31 162 views
0

我是新的JPA,當我從複合原始鍵表中檢索到值時,我面臨異常。jpa複合主鍵表不返回值

異常描述:

Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t]. 
    [14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown. 
     at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605) 

以下是我的代碼

@Entity 
     @Table(name = "ASSIGN_TASK_EMPLOYEE") 
     //@IdClass(AssignTaskEmployeePk.class) 
     public class AssignTaskEmployee implements Serializable { 

      @EmbeddedId 
      private AssignTaskEmployeePk assignTaskEmployeePk; 

      public AssignTaskEmployeePk getAssignTaskEmployeePk() { 
       return assignTaskEmployeePk; 
      } 

      public void setAssignTaskEmployeePk(AssignTaskEmployeePk assignTaskEmployeePk) { 
       this.assignTaskEmployeePk = assignTaskEmployeePk; 
      } 

     } 

     @Embeddable 
     public class AssignTaskEmployeePk { 

      private String employeeId; 
      private String taskId; 
      public AssignTaskEmployeePk() { 
      } 

      @Override 
      public boolean equals(Object obj) { 
       // TODO Auto-generated method stub 

       if (obj instanceof AssignTaskEmployeePk) { 

        AssignTaskEmployeePk employeePk = (AssignTaskEmployeePk) obj; 
        if (!employeePk.getEmployeeId().equals(this.employeeId)) { 
         return false; 
        }   
        else if (!employeePk.getTaskId().equals(this.taskId)) { 
         return false; 
        } 

       } 
       else { 
        return false; 
       } 

       return false; 

      } 

      @Override 
      public int hashCode() {  
       return employeeId.hashCode() + taskId.hashCode() ; 
      } 

      public String getEmployeeId() { 
       return employeeId; 
      } 

      public void setEmployeeId(String employeeId) { 
       this.employeeId = employeeId; 
      } 

      public String getTaskId() { 
       return taskId; 
      } 

      public void setTaskId(String taskId) { 
       this.taskId = taskId; 
      } 


      } 

我已經在數據庫中添加了四個值複合主鍵ASSIGN_TASK_EMPLOYEE(表)PK表

EMP_ID TASKID 
1   2 
2   4 
3   5 
4   6 

現在我想獲得分配給emp_id 1的任務爲此我寫了下面的查詢:這應該會返回AssignTaskEmployee對象的列表。

entityManager.createQuery("select t from ASSIGN_TASK_EMPLOYEE t").getResultList() 

當我執行此查詢,我得到以下異常

Exception Description: 

Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t]. 
      [14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown. 
       at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605) 

回答

3

JPQL應該使用實體的名稱,默認是類的名稱。 AssignTaskEmployee

應該

entityManager.createQuery("select t from AssignTaskEmployee t").getResultList() 

以上將在表ASSIGN_TASK_EMPLOYEE返回的所有記錄。

如果你想檢索與JPQL一個特定的記錄,你應該WHERE語句,因爲它遵循使用方法:

Query query = entityManager.createQuery("select t from AssignTaskEmployee t WHERE 
t.assignTaskEmployeePk.employeeId = :employeeId and t.assignTaskEmployeePk.taskId = :taskId") 

query.setParameter("employeeId", 1); 
query.setParameter("taskId",1); 

query.getSingleResult() //As expected to have only one record. 

this超過EmbeddedId查詢

+0

非常感謝!!!!!有用!! –