简体   繁体   English

如何使用 HQL 查找 OneToMany 映射中列的最大值?

[英]How to find the maximum value for a column in an OneToMany mapping, using HQL?

These are the classes Country and State :这些是CountryState类:

Country:国家:

 @Entity
    @Table(name="Country")
    public class Country{
        @Id
        private String countryName;
        private String currency;
        private String capital;
        @OneToMany(mappedBy="country", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
        private List<State> statelist = new ArrayList<State>();

State: State:

@Entity
 @Table(name="State")
 public class State{
     @Id
     private String stateName;
     private String language;
     private long population;
     @ManyToOne
     @JoinColumn(name="countryName")
     private Country country;

What should be the HQL query to retrieve the states (in a List perhaps) which have the highest population in a particular country?检索特定国家/地区人口最多的州(也许在列表中)的 HQL 查询应该是什么?

Here's the code I have written, where, I tried to retrieve the maximum population value first, and then run through all the states in that country, to match against every population value, and add states to a list.这是我编写的代码,其中,我尝试首先检索最大人口值,然后遍历该国家/地区的所有州,以匹配每个人口值,并将州添加到列表中。 But, while doing so, I get the error that the columns are ambiguously defined in the query.但是,在这样做的同时,我得到了列在查询中定义不明确的错误。

public List<State> stateWithMaxPopulation(String countryName){
    List<State> l = new ArrayList<State>();
    Country ctr = (Country)session.get(Country.class,countryName);
    String hql = "select max(stlst.population) from Country cntry "
    +" join cntry.statelist stlst where countryName=:cNm";

    Query query = session.createQuery(hql);
    query.setParameter("cNm", countryName);
    Long maxPop = (Long)query.uniqueResult();

    for(State st : ctr.getStatelist()){
        if(st.getPopulation() == maxPop)
            l.add(st);
    }

    return l;
}

What should be the correct way to do it?正确的做法应该是什么?

You are missing the alias for entity您缺少实体的别名

select max(stlst.population) from Country cntry " +" join cntry.statelist stlst where cntry.countryName=:cNm1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java在Excel工作表的列中找到最大值? - How to find maximum value in a column of an Excel sheet using java? 使用休眠OneToMany映射的未知列错误 - Unknown column error using hibernate OneToMany mapping 如何将@OneToMany 字段映射到列表中<DTO>什么时候使用 JPQL 或 HQL? - How to map @OneToMany field into List<DTO> when using JPQL or HQL? 如何使用HQL(休眠查询语言)获取列的最后一个值 - How to get the last value of a column using HQL(Hibernate Query Language) 使用OneToMany映射时在Hibernate的Join列中获取Null - Getting Null in Join Column in Hibernate when using OneToMany mapping 如何找到一列mysql的最大值并返回该值? - how to find the maximum value of a column mysql and return the value? 在Hibernet 1中通过使用联接列完成许多映射如何使用该联接列编写HQL查询以创建内部联接 - in Hibernet 1 to many mapping done by using join column how write HQL query to create inner join using that join column Hibernate如何在@OneToMany映射中找到集合的泛型类型? - How does Hibernate find the generic type of a collection in a @OneToMany mapping? 如何在休眠SQL(HQL)查询中引用联接列字段(@OneToMany情况)? - How to refer to the join column field (@OneToMany case) in a hibernate sql (HQL) query? 如何编写休眠OneToMany关系的hql查询? - how to write hql query for hibernate OneToMany relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM