简体   繁体   中英

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.

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')]

Hibernate Session's createQuery() method requires valid HQL syntax . You can check how to write joins here . Basically, in HQL you work with your entities, not SQL tables . So you don't need to write ON , because you already map association between entities.

If you still want to write native SQL query, you need to use session.createSQLQuery(); instead

Try to use session.createSQLQuery() instead.

and don't put enclosed apostrophe (''), you are inputting numbers , not 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+")"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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