0

哪个缺少以下内容JPQL query

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

1 回答 1

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);

这样您就不必考虑您的枚举在 DB 中是如何表示的。

于 2013-06-21T09:13:41.217 回答