这些是类别(国家和州):
国家:
@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>();
状态:
@Entity
@Table(name="State")
public class State{
@Id
private String stateName;
private String language;
private long population;
@ManyToOne
@JoinColumn(name="countryName")
private Country country;
HQL查询应该是什么以检索特定国家中人口最多的州(也许在列表中?)>
这是我编写的代码,在这里,我尝试首先检索最大人口值,然后遍历该国家/地区的所有州,以与每个人口值匹配,然后将州添加到列表中。但是,在执行此操作时,出现错误,即查询中的列定义不明确。
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; }
正确的方法是什么?
这些是类(国家和州):国家:@Entity @Table(name =“ Country”)公共类国家{@Id private String countryName;专用String货币; ...
您缺少实体的别名