用于此映射的Hibernate自定义HQL

时间:2011-10-03 10:25:12

标签: java hibernate spring orm

我有以下表格

------------
BAR
------------
ID
number

------------
ZOO
------------
ID

------------
FOO
------------
ID

------------
MAPPER
------------
ID
FOO
BAR
ZOO

现在我想获取特定FOO的所有动物园所以我会做

select ZOO from MAPPER where zoo = someZoo

但现在我希望这些ZOO根据投票号进行排序,因此SQL将是

SELECT FOOBAR.ZOO
FROM  mapper AS mapper,
BAR AS bar
WHERE mapper.FOO=SOME_VALUE AND mapper.BAR=bar.id order by bar.number desc

但是现在我想在Hibernate中以DB独立的方式这样做我该怎么办?

我有实体映射设置我正在使用Spring Hibernate模板支持

public class Foo{
Long id;
}


public class Zoo{
Long id;
}

public class Bar{
Long id;
Long num;
}

public class Mapper{
Long id;
Long foo;
Long bar;
Long zoo;

}

1 个答案:

答案 0 :(得分:2)

Class Mapper应该是:

public class Mapper {
    Long id;
    Foo foo;
    Bar bar;
    Zoo zoo;
}

现在,您可以编写以下HSQL查询:

select mapper.zoo from Mapper mapper where mapper.foo=:foo order by mapper.bar.num desc