Cuisine cusine=(Cuisine)getHibernateTemplate().get(Cuisine.class,cuisineId);
String hql="select A from Refectory as A,Cuisine as B where A.cuisine=?";
List l=getHibernateTemplate().find(hql, cusine);
餐厅类Refectory ,菜系类id字段cuisineId;

解决方案 »

  1.   

    @Override
    public Collection queryRes(String resname, String areaID, String cityID,
                       String cuisineID,String perconsume, String personcounts,
                       String bookingDate,String mealid,String tablecounts){
    Session session = hibernateTemplate.getSessionFactory().openSession();

    List list = new ArrayList();
    String hql = " FROM Restaurant a inner join a.cuisines as r, BookableTable b,ProvideDate c " +
         " WHERE a.id = b.restaurant.id AND " +
         " b.restaurant.id = c.restaurant.id AND " +
         " a.id = c.restaurant.id AND " +
         " c.bookableTable = b.id AND " +
         " a.acceptingBooking = 1 ";

    if(resname!= null && !(resname.length()==0)){
    hql += " AND a.name like ? "; 
        list.add("%" + resname + "%");
    }

    if(areaID!= null && !areaID.equals(null)){
    hql += " AND a.area.id = ? "; 
        list.add(Long.valueOf(areaID));
    }

    if(cityID != null && !cityID.equals(null)){
    hql += " AND a.city.id = ? "; 
        list.add(Long.valueOf(cityID));
    }

    if(cuisineID != null && !cuisineID.equals(null)){
    hql += "AND r.id = ? ";
    list.add(Long.valueOf(cuisineID));  
    }

    if(perconsume != null && !perconsume.equals(null)){
    if(!perconsume.equals("0")){
    if(perconsume.equals("1")){
    hql += " AND a.perconsume >= 50 AND a.perconsume <= 100"; 
    }

    if(perconsume.equals("2")){
    hql += " AND a.perconsume >= 101 AND a.perconsume <= 200"; 
    }

    if(perconsume.equals("3")){
    hql += " AND a.perconsume >= 201 AND a.perconsume <= 500"; 
    }

    if(perconsume.equals("4")){
    hql += " AND a.perconsume >= 501"; 
    }
    }
    }

    if(personcounts != null && !personcounts.equals(null)){
    hql += " AND b.maxCapitaPerTable >= ? AND b.minCapitaPerTable <=? AND b.state = 1 "; 
        list.add(Integer.valueOf(personcounts));
        list.add(Integer.valueOf(personcounts));
    }

    if(bookingDate != null && !bookingDate.equals(null)){
    hql += " AND c.rq = ? ";
    list.add(DateHelp.convertStringToDate("yyyy-MM-dd", bookingDate));
    }

    if(mealid != null && !mealid.equals(null)){
    hql += "AND c.mealProvideTimeType.id = ? ";
    list.add(Long.valueOf(mealid));
    }

    if(tablecounts != null && !tablecounts.equals(null)){
    hql += "AND c.surplusamount >= ? ";
    list.add(Integer.valueOf(tablecounts));
    }

    Query query = session.createQuery(hql);

    for(int i = 0; i<list.size();i++){
    query.setParameter(i, list.get(i));
    }

    ArrayList<Restaurant> resList = new ArrayList<Restaurant>();

    ArrayList<ProvideDate> provideList = new ArrayList<ProvideDate>();

    for(int i = 0; i< query.list().size(); i++){
    Object[] obj=(Object[])query.list().get(i);   
        for(int j=0;j<obj.length;j++){   
            if(obj[j] instanceof Restaurant){   
             Restaurant res = (Restaurant)obj[j];
             if(!resList.contains(res)){
             resList.add(res);
             }
            }else if(obj[j] instanceof ProvideDate){
             ProvideDate provideDate = (ProvideDate) obj[j];
            
             if(!provideList.contains(provideDate)){
             provideList.add(provideDate);
             }
            }
        } 
    }
    Collection all = new ArrayList();

    all.add(resList);
    all.add(provideList); return all;
    }