2011-10-03 69 views
1

我有如下表休眠定製HQL這種映射

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

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

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

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

現在我想獲取所有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中做到這一點我將如何去?

我有實體映射設置我使用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; 

} 
+0

您能否提供實體代碼,而不是僞數據庫表? –

+0

@James DW添加了基本模型類,沒有映射和東西 – user602865

回答

2

類映射應該是:

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