简体   繁体   English

将MySql查询转换为JPA命名查询

[英]Convert MySql query to JPA named query

I have a MySql query like this: 我有一个像这样的MySql查询:

select AK.*, max(AA.activityDate) 
from AssessmentKey AK 
join AssessmentActivity AA on AA.ASSESSMENTKEY_ID = AK.id 
group by AK.id having max(AA.activityDate) <=  '2012-10-02 17:30:55'

Is there a way to convert into in JPA NamedQuery . 有没有办法转换成JPA NamedQuery I am using OpenJPA . 我正在使用OpenJPA

If I put it directly into: 如果我把它直接放入:

@NamedQuery(name = "AssessmentActivity.findByDate", 
  query = "select AK.*, max(AA.activityDate) from AssessmentKey AK 
           join AssessmentActivity AA on AA.ASSESSMENTKEY_ID = AK.id 
           group by AK.id having max(AA.activityDate) <=  '2012-10-02 17:30:55'")

The error is showed here: select AK.* that identifier expected, got "*" and also it does not like on , here it says: 该错误是在这里显示: select AK.*identifier expected, got "*" ,也不会像on ,在这里它说:

在此输入图像描述

How can I resolve this problem? 我该如何解决这个问题?

First problem: you should replace AK.* with AK you just need the entity alias here. 第一个问题:你应该更换AK.*AK ,你只需要在这里的实体别名。

Second problem: join syntax is not like that. 第二个问题:连接语法不是那样的。 You should write: join and navigate through the object references,eg: AK.assesmentActivity and use the where keyword instead of on 您应该编写: join并浏览对象引用,例如:AK.assesmentActivity并使用where关键字而不是on

Here's a tip on join: JPQL join 这是关于加入的提示: JPQL加入

Remember: you are in the ORM, dealing with Entities and their properties, not DB foreign keys and columns. 请记住:您在ORM中处理实体及其属性,而不是DB外键和列。

(ps: Maybe you wanted to write a NativeQuery ? There you can use native SQL syntax) (ps:也许你想编写一个NativeQuery ?你可以使用本机SQL语法)

EDIT: (on your comment) So you must start your query from AA: 编辑:(在你的评论上)所以你必须从AA开始你的查询:

select AK from AssesmentActivity AA join AssesmentKey AK where AA.assesmentKey = AK ...

This way you can join them. 这样你就可以加入他们了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM