2013-07-16 133 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 

将返回所有的城市,你会首先找到阿塞拜疆的城市,那么这些比利时,那么加拿大等。