【问题标题】:Datetime to Date (MySQL) in JPA createQuery error Exception [EclipseLink-8025]JPA createQuery 中的 Datetime to Date (MySQL) 错误异常 [EclipseLink-8025]
【发布时间】:2013-05-09 15:19:28
【问题描述】:

我是 JAVA 和 JPA 的新手。 我使用命令

select * from asm.attendant where staff_no='0004' and node_code='KT1' and date(attendant_date) = '2013-05-13';

在 MySQL 中是有效的

但是

当我在 JPA 中使用时

    public List<Attendant> listAttendantByStaffNoAndNodeCodeAndDateWs(String staffNo,String nodeCode,String attendantDateData) throws ParseException {

    Date attendantDate = new SimpleDateFormat("yyyy-MM-dd").parse(attendantDateData);

    Query result =  em.createQuery("SELECT a FROM Attendant a WHERE a.staffNo = :staffNo AND a.nodeCode = :nodeCode AND DATE(a.attendantDate) = :attendantDate", Attendant.class);
    result.setParameter("staffNo", staffNo);
    result.setParameter("nodeCode", nodeCode);
    result.setParameter("attendantDate", attendantDate);
    return result.getResultList();
}  

这行不通。

错误

原因:异常 [EclipseLink-8025] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JPQLException 异常描述:语法错误解析查询 [SELECT a FROM Attendant a WHERE a.staffNo = :staffNo AND a.nodeCode = :nodeCode AND DATE(a.attendantDate) = :attendantDate],第 1 行,第 88 列:意外令牌 [( ]。 内部异常:NoViableAltException(83@[()* loopback of 383:9: (d= DOT right= attribute )*])

【问题讨论】:

    标签: jpa


    【解决方案1】:

    DATE() 不是有效的 JPQL。在 JPA 中 createQuery() 采用 JPQL 而不是 SQL。如果你想要 SQL 使用 createNativeQuery()。

    或者,在 JPQL 中,只需将日期标记为参数:date any 将参数设置为您的 java.sql.Date 实例。

    【讨论】:

      猜你喜欢
      • 2017-09-01
      • 2016-05-02
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      相关资源
      最近更新 更多