【问题标题】:Hibernate custom HQL for this mapping用于此映射的 Hibernate 自定义 HQL
【发布时间】:2011-10-03 10:25:11
【问题描述】:

我有以下表格

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

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

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

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

现在我想获取特定 FOO 的所有 ZOO 所以我会这样做

select ZOO from MAPPER where zoo = someZoo

但现在我希望这些动物园根据投票数排序,这样 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;

}

【问题讨论】:

  • 能否提供实体代码,而不是伪数据库表?
  • @James DW 添加了没有映射和东西的基本模型类

标签: java hibernate spring orm


【解决方案1】:

类映射器应该是:

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

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2012-12-23
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多