2013-07-16 131 views
0

我試圖打印由國家劃分城市與Java +彈簧+的一些列表休眠+碧玉報告,但,但有一些問題與填充城市的分組列表組...休眠:通過

我有以下查詢:

Query query = session.createQuery("SELECT city FROM DictionaryCity city JOIN city.country as country GROUP BY country ORDER BY country.name ASC"); 

,就是始終返回定義如下方式的城市,但使用1x1連接,但DictionaryCity類的列表:

@Entity 
@Table(name = "dictionarycities") 
public class DictionaryCity implements IDictionary { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 3638441397996216204L; 

    /** 
    * 
    */ 
    @Id 
    @Column(name = "Id") 
    @GeneratedValue 
    private Long id = null; 

    /** 
    * 
    */ 
    @Column(name = "Name") 
    private String name; 

    /** 
    * 
    */ 
    @ManyToOne(targetEntity = DictionaryCountry.class) 
    @JoinColumn(name = "CountryId") 
    private DictionaryCountry country; /** other... **/ } 

有什麼不對?謝謝

+0

什麼是生成的SQL? – RAS

回答

1

group by與返回相似的行組合在一起沒有任何關係。當選擇子句使用聚合函數如SUM它的使用,最小值,最大值,計數等

例如,查詢

select country.id, sum(city.id) from Country country left join country.cities city 

回報每一個國家包含在這個國家的城市數量沿。

如果你只是想擁有按國家城市羣,order by就是你需要:

select city FROM DictionaryCity city JOIN city.country as country order by country.name ASC 

將返回所有的城市,你會首先找到阿塞拜疆的城市,那麼這些比利時,那麼加拿大等。