JPA2与EclipseLink 2.7.3中EXTRACT函数报错问题求助
Alright, let's get to the bottom of this confusing error you're facing. The root cause is a simple JPQL syntax mistake in how you're using the EXTRACT function—EclipseLink's query parser isn't recognizing it as a function call, which is why it's treating EXTRACT like an undefined identification variable.
What Went Wrong
You wrote the EXTRACT function with a comma separator:
EXTRACT(DAY,s.lastExecutionDate)
But the correct JPQL syntax for EXTRACT (supported by JPA 2.1 and EclipseLink 2.7.x) requires using the FROM keyword to separate the date component and the date expression. When you use a comma, the parser misinterprets EXTRACT as a variable that should be declared in the FROM clause—hence the error message you're seeing.
Corrected Named Query
Here's the fixed version of your query, with the proper EXTRACT syntax:
@NamedQuery( name = User.FIND_USER_CWC_ERROR_TODAY, query = "select u from User u, Score s " + "where u.id = s.userId " + "and s.status = com.model.Status.ERROR " + "and s.scoreType = com.model.ScoreType.CREDIT " + "and EXTRACT(DAY FROM s.lastExecutionDate) = EXTRACT(DAY FROM CURRENT_DATE)" )
Quick Note on EXTRACT Usage
EclipseLink 2.7.3 fully supports the JPA 2.1 EXTRACT function, which follows this standard format:
EXTRACT(<date_component> FROM <date_expression>)
Valid date components include DAY, MONTH, YEAR, HOUR, MINUTE, SECOND, etc. Using this syntax ensures the parser correctly identifies it as a function rather than an undefined variable.
内容的提问来源于stack exchange,提问作者cyril

