【问题标题】:HQL how to join two tables with out any associationHQL如何在没有任何关联的情况下连接两个表
【发布时间】:2013-11-15 00:45:32
【问题描述】:

我已经四处搜索,一些答案正在使用标准生成器,我的问题是在 HQL 中。 我有两个表 A 和 B,它们无论如何都没有关联,但我需要从每个字段中匹配一个才能从表 B 中获取 fieldB2 的值。

我的查询:

Table A - fieldA1 (ex. value of '20'), fieldA2 A
Table B - fieldB1 (ex. value of '20'), fieldB2 B

这不起作用:

select new map (fieldA1 as fa, (select fieldB1 from B as b where a.fieldA1=b.fieldB1) as fb) from A as a

所以基本上,我需要从 B 获取 fieldB1,如果它包含的值与来自 A 的 fieldA1 的值相同。

这样做的正确方法是什么?

【问题讨论】:

    标签: sql join hql


    【解决方案1】:

    在没有关联的情况下,无法使用 Criteria 编写 HQL 或查询。

    使用原生 SQL 查询来检索记录。

    【讨论】:

    • 我认为可能是这种情况,但是有没有不使用本机 sql 的解决方法?谢谢
    • 没有。没有解决方法。很简单,如果您编写 HQL,hibernate 会将其转换为 SQL。但是如果没有关联(关系),hibernate 怎么知道两个类是如何关联的呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多