简体   繁体   English

在hibernate中加入和where子句

[英]join and where clause in hibernate

I am using join and where clause in hibernate 3.but i cant reach the solution.I got the error. 我在hibernate 3中使用join和where子句但是我无法达到解决方案。我收到了错误。

Query qry= session.createQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
        + "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='"+id+"')");

        List l = qry.list();
        Iterator it=l.iterator();

        while(it.hasNext())
        {

            Object rows[] = (Object[])it.next();
            System.out.println(rows[0]+separator+rows[1]+separator+rows[2]+separator+rows[3]+separator+rows[4]);
        }

Issue: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1, column 127 [SELECT addemployee.eid,addemployee.fname,addemployee.location,empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='206')] 问题:org.hibernate.hql.ast.QuerySyntaxException:意外令牌:ON第1行第127列[SELECT addemployee.eid,addemployee.fname,addemployee.location,empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee .eid = empdet.eid WHERE(addemployee.eid ='206')]

Hibernate Session's createQuery() method requires valid HQL syntax . Hibernate Session的createQuery()方法需要有效的HQL语法 You can check how to write joins here . 您可以在此处查看如何编写联接。 Basically, in HQL you work with your entities, not SQL tables . 基本上,在HQL中,您使用的是实体,而不是SQL表 So you don't need to write ON , because you already map association between entities. 所以你不需要写ON ,因为你已经映射了实体之间的关联。

If you still want to write native SQL query, you need to use session.createSQLQuery(); 如果您仍想编写本机SQL查询,则需要使用session.createSQLQuery(); instead 代替

Try to use session.createSQLQuery() instead. 尝试使用session.createSQLQuery()代替。

and don't put enclosed apostrophe (''), you are inputting numbers , not varchar . 并且不要放入包含的撇号(''),而是输入numbers ,而不是varchar

like this. 像这样。

Query qry= session.createSQLQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
    + "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ="+id+")"); 

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

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