带有子查询的SQL到HQL

时间:2010-11-23 20:20:49

标签: sql hibernate hql

我已经读过有关使用HQL的子查询缺乏支持,但无论如何,任何人都可以告诉我这个SQL查询是否可以用HQL实现?

我已经做了一些测试,但没有成功......不知道我是否走在正确的道路上......

SQL(工作)


    SELECT
    foo.id as fooId,
    foo.name AS fooName,
    chan.name AS chanName,
    (SELECT DISTINCT
        foo_lang.VALUE
     FROM
        foo_lang
     WHERE
        foo_lang.foo_id = fooId AND
        foo_lang.lang_id = 1) as EN
    FROM
        foo
    INNER JOIN chan ON foo.chan_id = chan.id

HQL(不工作)


select new
    Map(o.id as id,
        o.name as fooName,
        c.name as chanName,
        (select fl.value from foo_lang fl where fl.id.fId = id and fl.id.lId = 1) as EN)
from
    foo o
    left join o.chan c

它给出了:

org.hibernate.TypeMismatchException:二进制逻辑运算符的左侧和右侧不兼容[big_decimal:component [lId,rId]]

是关于子查询还是别的什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我试着用一点点不同的对象重现你的情况,我不明白为什么你需要这个子查询。 AFAIK,从逻辑上讲,如果你将 foo_lang 加入 foo 就会一样,就像这样:

SELECT
 o.id,
 o.name as fooName,
 c.name as chanName,
 fl.value as EN
FROM foo AS o
LEFT JOIN o.chan AS c
LEFT JOIN o.foo_lang AS fl WITH fl.lang_id = 1