大哥们解决一个问题、在HQL中写省市级联、省市的hql查询语句写好了、还差县的、现在我以市的名字作条件、我想把县查出来、这个HQL查询语句怎么写啊?在配置文件类中我都设了单向一对多的关联属性、下面是我写的省的HQL语句
public ArrayList<Province> getProvince()
{  
session=this.getSession();
String hql="select from Province";//省的HQL语句
ArrayList<Province> al=new ArrayList<Province>();
List l=session.createQuery(hql).list();
 Iterator it=l.iterator();
while (it.hasNext())
{
Province p=(Province) it.next();
int i=p.getProvince_id();
p.setProvince_id(i);
String name=p.getProvince_name();
p.setProvince_name(name);
al.add(p); }
this.closeSession();
return al;
}
//通过省ID查询所有的市
public ArrayList<City> getCity(int pid)
{  
session=this.getSession();
String hql="select from City where province_id=:pid";//市的HQL语句
List list=session.createQuery(hql).list();
ArrayList<City> al=new ArrayList<City>();
Iterator it=list.iterator();
while (it.hasNext())
{   
City p=(City) it.next();
int id=p.getProvince_id();
p.setProvince_id(id);

String name=p.getCity_name();
p.setCity_name(name);
p.setCity_id(p.getCity_id());
al.add(p);
}

this.closeSession();

return al;
}
//通过市的名字查询所有的县
public ArrayList<County> getCounty(String cname)
{   
session=this.getSession();
     //这里要关联到两个表、请问怎关联?下面这种做法对吗?不对请纠正、请写出代码、谢谢!
String hql="select c from County c join City cc c.city_id=cc.city_id where city_name=:cname";
ArrayList<County> al=new ArrayList<County>();
List list=session.createQuery(hql)
.setParameter(0, cname)
.list();
Iterator it=list.iterator();
while (it.hasNext())
{
County p=(County) it.next();
int id=p.getCity_id();
p.setCity_id(id);
String name=p.getCounty_name();
p.setCounty_name(name);
p.setCounty_id(p.getCounty_id());
al.add(p);
} this.closeSession();
return al;
}请大家帮个忙、解决了60分全给你、求详解!

解决方案 »

  1.   

    String hql="select c from County c ,City cc where  c.city_id=cc.city_id and city_name=:cname";
      

  2.   

    就和普通的sql语句一样,把三张表连接起来
      

  3.   

    关联是写好了、、String hql="select c from County c ,City cc where c.city_id=cc.city_id and city_name=:cname"; 我的HQL语句是写好了、我不知道对不 、哎、还是去测试下、谢谢你们了、我短路了、不记得测试了、测试下就知道是对是错了、、呵呵