gpt4 book ai didi

hibernate - 将 SQL 转换为 HQL

转载 作者:行者123 更新时间:2023-12-03 07:45:13 25 4
gpt4 key购买 nike

我正在尝试将以下 SQL 查询转换为 HQL,但遇到了一些问题。逐行直线转换不起作用,我想知道是否应该在 HQL 中使用 Inner Join?

        SELECT (UNIX_TIMESTAMP(cosc1.change_date) - UNIX_TIMESTAMP(cosc2.change_date)) 
FROM customer_order_state_change cosc1
LEFT JOIN customer_order_state cos1_new on cosc1.new_state_id = cos1_new.customer_order_state_id
LEFT JOIN customer_order_state cos1_old on cosc1.old_state_id = cos1_old.customer_order_state_id
LEFT JOIN customer_order_state_change cosc2 on cosc2.customer_order_id = cosc1.customer_order_id
LEFT JOIN customer_order_state cos2_new on cosc2.new_state_id = cos2_new.customer_order_state_id
LEFT JOIN customer_order_state cos2_old on cosc2.old_state_id = cos2_old.customer_order_state_id
WHERE cos1_new.name = "state1" AND cos2_new.name = "state2" and cosc2.change_date < "2008-11-06 09:00"
AND cosc2.change_date > "2008-11-06 06:00" GROUP BY cosc1.change_date, cosc2.change_date ;

查询返回客户订单状态更改之间的时间(以秒为单位)。

州名称和日期会动态插入到查询中。

编辑:刚刚尝试过这个

"SELECT (UNIX_TIMESTAMP(cosc1.changeDate) - UNIX_TIMESTAMP(cosc2.changeDate))" + 
" FROM" +
" " + CustomerOrderStateChange.class.getName() + " as cosc1" +
" INNER JOIN " + CustomerOrderStateChange.class.getName() + " as cosc2" +
" WHERE cosc1.newState.name = ?" +
" AND cosc1.order.id = cosc2.order.id" +
" AND cosc2.newState.name = ?" +
" AND cosc2.changeDate < ?" +
" AND cosc2.changeDate > ?" +
" GROUP BY cosc1.changeDate, cosc2.changeDate";

并收到异常“外部连接或完整连接后面必须跟有路径表达式

最佳答案

通常,您的 HQL 连接是使用对象上的属性指定的,例如,如果类 Foo 和 Bar 且 Foo.bar 的类型为 Bar,则 from Foo f inside join f.bar as b是连接。据我所知,没有办法在 HQL 中执行自连接(我在这里可能是错的)。

也就是说,Hibernate 允许您使用 session.createSQLQuery(...) 编写(稍微增强的)SQL 查询。

关于hibernate - 将 SQL 转换为 HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/267085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com