2013-05-15 33 views
0

我是JAVA和JPA中的新成員。 我在MySQL中使用命令JPA createQuery錯誤中的日期時間(MySQL)異常[EclipseLink-8025]

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

它的工作

,當我在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持久服務 - 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)*])

回答

2

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

或者,在JPQL中只需將日期標記爲參數:date any將參數設置爲您的java.sql.Date實例。