2013-06-21 54 views
0

缺少以下內容JPQL query帶有IN運算符的枚舉列表,JPQL

SELECT h FROM WorkFlowHistory h WHERE h.referenceNo = :referenceNo 
AND h.workflowTask IN ('CONFIRMATION','PAYMENT','ISSUING') 

錯誤:

invalid IN expression argument [CONFIRMATION] 

動態查詢方法

public List<WorkFlowHistory> findWorkFlowHistoryByRefNo(String refNo, WorkflowTask ...workflowTasks) { 
    ... 
    StringBuffer buffer = new StringBuffer("SELECT h FROM WorkFlowHistory h WHERE h.referenceNo = :referenceNo"); 
    if(workflowTasks != null && workflowTasks.length > 0) { 
     buffer.append(" AND h.workflowTask IN ("); 
     for (int i = 0; i < workflowTasks.length; i++) { 
      buffer.append("'" + workflowTasks[i] + "'"); 
      if ((i + 1) != workflowTasks.length) { 
       buffer.append(","); 
      } 
     } 
     buffer.append(")"); 
    } 
    Query q = em.createQuery(buffer.toString()); 
    .... 
} 

我的枚舉類

public enum WorkflowTask { 
    UNDERWRITING("Underwriting"), 
    SURVEY("Survey"), 
    APPROVAL("Approval"), 
    INFORM("Inform"), 
    CONFIRMATION("Confirmation"), 
    PAYMENT("Payment"), 
    PROPOSAL_REJECT("Proposal Reject"), 
    ISSUING("Issuing"); 

    private String label; 

    private WorkflowTask(String label) { 
     this.label = label; 
    } 

    public String getLabel() { 
     return label; 
    } 
} 

回答

2

我認爲,問題出在DB你枚舉的表示。你的枚舉怎麼映射到數據庫OrdinalString

最好的將只是把你的枚舉作爲參數佔位符的JPQL查詢:

SELECT h FROM WorkFlowHistory h WHERE h.referenceNo = :referenceNo 
AND h.workflowTask IN :workflowTasks 

比將其作爲參數來查詢,像這樣:

... 
List<WorkflowTask> workflowTasks=Arrays.asList({WorkflowTask.CONFIRMATION, WorkflowTask.PAYMENT WorkflowTask.ISSUING }); 
... 
Query q = em.createQuery(buffer.toString()); 
q.addParameter("workflowTasks", workflowTasks); 

這樣您不必考慮如何在數據庫中代表您的枚舉。

+0

Thz,我也在這裏得到它:http://stackoverflow.com/questions/2772305/jpql-in-clause-java-arrays-or-lists-sets謝謝你的支持 – CycDemo