编程语言
首页 > 编程语言> > java – 带有CONCAT的NamedQuery返回错误

java – 带有CONCAT的NamedQuery返回错误

作者:互联网

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

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已经根据连接列中的值进行了排序.

谢谢

解决方法:

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

标签:java,concatenation,jpa,eclipselink,named-query
来源: https://codeday.me/bug/20190626/1288922.html