1. 程式人生 > >An Example of association query

An Example of association query

................................. 

 String hql="select task,insL.InsName,insR.InsName,tra"
        +" from Institution as insL,Transition as tra,Institution as insR,Task as task"
        +" where task.id=tra.taskId"
        +" and insL.InsId=tra.foreNode"
        +" and insR.InsId=tra.aftNode"
        +(condition==null?(""):condition);
  List list=null;
    try{
   Query query=session.createQuery(hql);
   list=query.list();tx.commit();
  }
  catch(Exception e){

  tx.rollback();
   e.printStackTrace();
   list=null;
  }
  finally{
   session.close();
  }
  if(list!=null){
   for(int i=0;i<list.size();i++){
    TransitionDTO transitionDTO=new TransitionDTO();
    Object[] obs=(Object[])list.get(i);
    Task tmp_task=(Task)obs[0];
    transitionDTO.taskId=tmp_task.getId();
    transitionDTO.foreInsName=(String)obs[1];
    transitionDTO.aftInsName=(String)obs[2];
      Transition tmp_trs=(Transition)obs[3];
    transitionDTO.transitionId=tmp_trs.getId();

.....................

注意HQL語句中from後面為類名,"."號後面為屬性名,必須和hbm.xml中的相一致,而不是和類名中的屬性名相同(儘管很多時候他們是相同的,但HQL語句中的必須和hbm中相一致)

the above code works,while the hbm.xml files don't contain the <many-to-one>properties