简体   繁体   English

如何在Hibernate的本机SQL中编写联接查询?

[英]How to write join query in native SQL of Hibernate?

List<AddHotelBean> list=new ArrayList<AddHotelBean>();
List<HotelFacilities> list1=new ArrayList<HotelFacilities>();
public String execute(){

    Configuration cfg=new Configuration();
    cfg.configure();
    SessionFactory sf=cfg.buildSessionFactory();
    Session session=    sf.openSession();

    SQLQuery q=session.createSQLQuery("select d.name,d.country,f.monday,f.tuesday from hotel.hoteldetails1 d , hotel.hotelfacilities1 f where d.hotelid=f.hotelid;");


    q.addScalar("name", StandardBasicTypes.STRING);
    q.addScalar("country", StandardBasicTypes.STRING);
    q.addScalar("monday", StandardBasicTypes.STRING);
    q.addScalar("tuesday", StandardBasicTypes.STRING);

    q.setResultTransformer(Transformers.aliasToBean(HotelFacilities.class));

    List<HotelFacilities> l=q.list();

    for(HotelFacilities a:l) {
        a.getMonday();
        a.getTuesday();
        list1.add(a);
    }
org.hibernate.QueryParameterException: could not locate named parameter [hotelid]

I'm getting this exception plz any one help me 我收到此例外,请有人帮忙

Actually my doubt is i am using two pojo classes but my Transformers.aliasToBean is only one class how it actually take two class thing to print in my output jsp page 其实我的疑问是我正在使用两个pojo类,但是我的Transformers.aliasToBean只是一个类,实际上如何在输出jsp页面中打印两个类的东西

thanks in advance. 提前致谢。

` `

I think you are confused with the parameters. 我认为您对参数感到困惑。 you call setParameter() when you want to pass some value in the query. 要在查询中传递某些值时,可以调用setParameter()

SQLQuery q=session.createSQLQuery("select d.name from hotel.hoteldetails1 d where d.hotelid=:hotelidd");
q.setParameter("hotelid", 1);

You don't require any value in the query so you don't need to call setParamert at all. 您在查询中不需要任何值,因此根本不需要调用setParamert。 Try this 尝试这个

SQLQuery q=session.createSQLQuery("SELECT d.name, d.country, f.monday, f.tuesday FROM hoteldetails1 d JOIN hotelfacilities1 f ON d.hotelid=f.hotelid");
q.setResultTransformer(Transformers.aliasToBean(HotelFacilities.class));
List<HotelFacilities> l=q.list();

Edit 编辑

Remove ; 删除; from the query. 从查询中。

You're using the setParameter method to put values into named parameters, but you're not putting any named parameters into your query string. 您正在使用setParameter方法将值放入命名参数中,但没有在查询字符串中放入任何命名参数。 You use the ":" syntax to do that. 您可以使用“:”语法来实现。

I can't figure out what your query is, so I don't know if you actually want named parameters. 我不知道您的查询是什么,所以我不知道您是否真正想要命名参数。 Guessing from your result transformer, I think you want a list of HotelFacilities objects. 从结果转换器猜测,我认为您想要一个HotelFacilities对象的列表。

Also, you're using some very awkward syntax, which I'll replace in this example. 另外,您使用的语法很尴尬,在本示例中将替换该语法。

from HotelFacilities f
join HotelDetails d
where d.name = :name
  and d.country = :country
  and f.monday = :monday
  and f.tuesday = :tuesday

This query returns all HotelFacilities where the monday property and tuesday property match the named parmeters, and where the matching HotelDetails matches the provided name adn country properties. 此查询返回所有HotelFacilities,其中monday属性和tuesday属性与命名的参数匹配,并且匹配的HotelDetails与提供的名称和国家/地区属性匹配。 You don't have to join on HotelID since (I'm presuming) that's already handled in your mapping. 您不必加入HotelID,因为(我想)这已经在您的映射中处理过。

To set those named parameters in your java code, use the correct typed method, in this case setString . 要在Java代码中设置这些命名参数,请使用正确的类型化方法,在本例中为setString

q.setString("name", detailsName);
q.setString("country", detailsCountry);
q.setString("monday", facilitiesMonday);
q.setString("tuesday", facilitiesTuesday);

//how we write join query in jdbc is same as hibernate //我们在jdbc中编写联接查询的方式与休眠方式相同

//local level
ArrayList<Object[]> data = new ArrayList<Object[]>();
Map request;


//method level
SQLQuery q=session.createSQLQuery("select  h.roomid as roomid, h.phone1 as     phone1,h.phone2 as phone2,r.type as type,r.nrooms as nrooms from hoteldetails1 h,roomdetails1 r where h.roomid=r.roomid ");

    System.out.println("after query");
    //q.setParameter(0,roomid);


    q.addScalar("roomid",StandardBasicTypes.INTEGER);
    q.addScalar("phone1",StandardBasicTypes.STRING);
    q.addScalar("phone2",StandardBasicTypes.STRING);
    q.addScalar("type",StandardBasicTypes.STRING);
    q.addScalar("nrooms",StandardBasicTypes.STRING);

    List<Object[]> l=q.list();

    System.out.println("b4 for loop");
    for(Object[] obj:l){



        data.add(obj);
        request.put("l",data);

    }

  return "success";


 /*
 in the jsp page how to retrive the output is given below
*/

//in jsp page code is like this
<%


List<Object[]> l =(List) request.getAttribute("l");



%>

 <table border=1>
 <tr>
 <th>roomid</th>
 <th>phone1</th>
 <th>phone2</th>
 <th>type</th>
 <th>rooms</th>

 </tr>  
 <%
  for(Object[] obj:l){


  int i=(Integer)obj[0];
  String j=(String)obj[1];
  String k=(String)obj[2];
  String r=(String)obj[3];
  String m=(String)obj[4];
  out.println(obj[0] +"   "+i);
  out.println(obj[1] +"   "+j);
  out.println(obj[2] +"   "+k);
  out.println(obj[3] +"   "+r);
  out.println(obj[4] +"   "+m); 

    %>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM