Vasuta
Vasuta

Reputation: 141

Datetime to Date (MySQL) in JPA createQuery error Exception [EclipseLink-8025]

I'am new in JAVA and JPA. I use command

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

in MySQL it's work

but

when i use in 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();
}  

it's not work.

error

Caused by: Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query [SELECT a FROM Attendant a WHERE a.staffNo = :staffNo AND a.nodeCode = :nodeCode AND DATE(a.attendantDate) = :attendantDate], line 1, column 88: unexpected token [(]. Internal Exception: NoViableAltException(83@[()* loopback of 383:9: (d= DOT right= attribute )*])

Upvotes: 0

Views: 1374

Answers (1)

James
James

Reputation: 18379

DATE() is not valid JPQL. In JPA createQuery() takes JPQL not SQL. If you want SQL use createNativeQuery().

Or, in JPQL just mark the date as a parameter :date any set the parameter to your instance of java.sql.Date.

Upvotes: 2

Related Questions