如何在jpa中编写本机查询

时间:2013-09-02 18:02:27

标签: jpa

按下搜索按钮时出现以下错误:

  

发生错误:

     

异常[EclipseLink-4002](Eclipse持久性服务 -   2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DatabaseException内部   例外:java.sql.SQLException:缺少IN或OUT参数   index :: 1错误代码:17041调用:SELECT * FROM CRM_DAILY_SHEET WHERE   to_char(reported_on,'dd-MM-yy')=:reportingOn查询:   ReadAllQuery(referenceClass = CrmDailySheet sql =“SELECT * FROM   CRM_DAILY_SHEET WHERE to_char(reported_on,'dd-MM-yy')=:reportedOn“)

代码:

public List < CrmDailySheet > searchres() throws Exception {
    SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd/MM/yy");
    Date d1 = searchsht.getReportedOn();
    String date_to_string = ddMMMyyFormat.format(d1);
    return crmsrvc.searchDailysht(date_to_string);
}

try {
    Query query = em.createNativeQuery("SELECT * FROM CRM_DAILY_SHEET  
 WHERE   to_char  (reported_on,'dd-MM-yy')    =      :reportedOn", CrmDailySheet.class);
    query.setParameter("reportedOn", reportedOn);
    List < CrmDailySheet > res = query.getResultList();
    return res;
} finally {
    em.close();
}

有人能找到解决方案吗?

1 个答案:

答案 0 :(得分:2)

您正尝试将JPQL参数语法(“:reportedOn”)与原生查询一起使用。请尝试使用?代替并按位置设置参数值,例如:

Query query = em.createNativeQuery("SELECT * FROM CRM_DAILY_SHEET
    WHERE to_char (reported_on,'dd-MM-yy') = ?", CrmDailySheet.class);
query.setParameter(1, reportedOn);