4

我正在尝试执行如下选择查询:

SELECT *, CONCAT(TNUSERDATE, ' - ', TNUSERTIME) AS datetime 
  FROM TODONOTE WHERE TNTDKEY='TD00019148' ORDER BY datetime DESC;

该查询有效;但是,当我尝试将其转移到命名查询中时:

@NamedQueries({
    @NamedQuery(name = "Todonote.findAll", query = "SELECT c FROM Todonote c"),
    @NamedQuery(name = "Todonote.findByTaskNumber", query = "SELECT c, CONCAT(c.userDate, ' - ', c.userTime) AS datetime FROM Todonote c WHERE c.todoTask = :taskNumber ORDER BY datetime DESC")
})

我收到一个错误

[EL Severe]: 2012-08-23 12:15:39.111--ServerSession(841933)--Local Exception Stack: 
Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.parsing.ConcatNode cannot be cast to org.eclipse.persistence.internal.jpa.parsing.AliasableNode].
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.parsing.ConcatNode cannot be cast to org.eclipse.persistence.internal.jpa.parsing.AliasableNode
Query: JPAQuery(name="Todonote.findByTaskNumber" )

我不确定问题是什么或我将如何执行 concat 语句。我的目标是运行 namedQuery 并且返回给我的 ResultList 已经根据连接列中的值进行了排序。

谢谢你

4

1 回答 1

4

JPA 1.0 和 JPA 2.0 有所不同。在 JPA 2.0 中,选择项中包含 1.0 中缺少的“[AS] result_variable”。所以你可以这样尝试;

  SELECT c, CONCAT(c.userDate, ' - ', c.userTime) FROM Todonote c WHERE c.todoTask = :taskNumber ORDER BY CONCAT(c.userDate, ' - ', c.userTime) DESC
于 2012-08-24T06:53:52.303 回答