【发布时间】:2017-04-25 11:21:02
【问题描述】:
我有一个使用 angular 和 java 制作的应用程序,我尝试从数据库中获取数据。因此,我尝试在 hql(休眠)中获取我的数据,但对于同一查询,我在 hql 和 sql 中没有相同的结果。
这是我的 sql 查询:
SELECT
co.label,
sol.material_code,
sum(sol.quantity),
sol.level
FROM sales_order so
JOIN sales_order_line sol ON so.root_so_id = sol.root_so_id
JOIN customer cu ON cu.id = so.id_customer
JOIN country co on co.id = cu.id_country
WHERE so.level = 1
AND sol.material_code in ('AR1MA010', 'VJCNS102088')
GROUP BY co.label, so.level;
这是我的 hql 查询:
select
co.label,
sol.materialCode,
sum(sol.quantity),
sol.level
from SalesOrder so
join so.salesOrderLines sol on so.rootSoId = sol.rootSoId
join so.customer cu
join cu.country co
where so.level = 1
and sol.materialCode IN ('AR1MA010', 'VJCNS102088')
group by co.label, sol.level
我尝试在控制台中运行这些查询。所以,我的代码没有问题,但我得到了 2 个不同的结果。
对于 sql 查询,我有:
Argentina AR1MA010 16000 2
Brazil VJCNS102088 20 1
Romania VJCNS102088 12 2
对于 hql 查询我有:
Brazil VJCNS102088 20 1
Romania VJCNS102088 1 1
你知道为什么吗?
【问题讨论】:
-
运行 HQL 查询时控制台中打印的 sql 是什么?将其与您的普通 sql 进行比较,您可以找出差异。
-
您需要添加实体类。同时启用 Hibernate 日志记录以查看 Hibernate 生成的原始 SQL。
-
如何在 intelliJ 中做到这一点?
-
join so.salesOrderLines sol on so.rootSoId = sol.rootSoId 在我看来是错误的。如果没有 ON 子句,这难道不应该工作吗?