Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用jpa与子查询进行内部联接_Mysql_Sql_Jpa - Fatal编程技术网

Mysql 使用jpa与子查询进行内部联接

Mysql 使用jpa与子查询进行内部联接,mysql,sql,jpa,Mysql,Sql,Jpa,我想将sql查询转换为jpa查询 我的两张桌子 看门狗 +---+---------------------+------+ |id |datetime | stid | +---+---------------------+------+ |1 | 2011-12-12 09:27:24 |1 | |2 | 2011-12-13 09:27:31 |2 | |3 | 2011-12-14 09:27:34 |4 | |4 | 2011-1

我想将sql查询转换为jpa查询

我的两张桌子

看门狗

+---+---------------------+------+
|id |datetime             | stid |
+---+---------------------+------+
|1  | 2011-12-12 09:27:24 |1     |
|2  | 2011-12-13 09:27:31 |2     |
|3  | 2011-12-14 09:27:34 |4     |
|4  | 2011-12-14 09:28:21 |2     |
+-----------------------+------+

station
+----+------+
| id | name |
+----+------+
|  1 | x    |
|  2 | xx   |
|  4 | yy   |
|  7 | z    |
+----+------+
我的班级

public class watchdog{
    @OneToOne
    @JoinColumn(name = "stid")
    private Station station;
    ...
}
我已经做了一个sql查询

SELECT wc.stid, st.name
FROM watchdog wc
inner join station st on wc.stid = st.id
inner join (
   select wc2.stid, max(wc2.datetime) as max_date_time from  watchdog wc2 group by     wc2.stid
) ms on wc. stid = ms.stid and wc.datetime = ms.max_date_time
我没有找到任何信息来知道是否有可能在jpa中这样做

我得到了昂贵的解决方案

entityManager.createQuerysb.toString=>目标VM中发生异常:org.hibernate.hql.ast.QuerySyntaxException: 预期加入的路径

SELECT wc
FROM com.xxx.entity.Watchdog wc
join fetch Station st
where wc.datetime = (
  select max(wc2.datetime) from  com.xxx.entity.Watchdog wc2 where wc.stid = wc2.stid   group by wc2.stid
);

)


我没有错误,但我看到mysql进程需要100%的cpu时间

您可以尝试将条件放在where而不是内部连接中,如下所示:

SELECT wc
FROM watchdog wc
join fetch station st
where wc.datetime = (
   select max(wc2.datetime) from  watchdog wc2 where wc.stid = wc2.stid group by wc2.stid
);

您可以通过在查询返回的看门狗实例上调用getStation.getName来获得它。完整错误是:entityManager.createQuerysb.toString=>目标VM中发生异常:org.hibernate.hql.ast.QuerySyntaxException:应加入的路径!从com.xxx.entity.Watchdog wc join fetch Station st中选择wc,其中wc.datetime=从com.xxx.entity.Watchdog wc2中选择maxwc2.datetime,其中wc.stid=wc2.stid按wc2.stid分组;你能不能也和我分享一下你的站牌课?JPA找不到看门狗和电台之间的任何连接。在电台中,并没有到看门狗的链接。在watchdog中有一个指向stationLooking数据的链接,我认为这种关系是manytone,而不是watchdog中指定的one。我认为JPA是在抱怨,因为这是一种一网打尽的关系,和车站的看门狗并没有任何联系。
SELECT wc
FROM watchdog wc
join fetch station st
where wc.datetime = (
   select max(wc2.datetime) from  watchdog wc2 where wc.stid = wc2.stid group by wc2.stid
);