2014-02-21 64 views
1

工作我使用hibernate-session.createQuery()執行的inner-join查詢,我得到了錯誤:連接查詢不處於休眠

Feb 21, 2014 5:22:07 PM org.hibernate.hql.internal.ast.ErrorCounter reportError 
ERROR: line 1:151: unexpected token: on 
Feb 21, 2014 5:22:07 PM org.hibernate.hql.internal.ast.ErrorCounter reportError 
ERROR: line 1:151: unexpected token: on 
line 1:151: unexpected token: on 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1693) 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1348) 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1054) 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:700) 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:294) 
    at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:157) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:266) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:106) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:81) 
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) 
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:217) 
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:195) 
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1649) 
    at com.sanyasi.masterDataActions.ModifyDistrictsAction.display(ModifyDistrictsAction.java:41) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255) 
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) 

我的查詢是:

select dist.districtName, dist.status, stat.stateName from sa_district_master dist inner join sa_state_master stat on dist.stateId = stat.stateId 

但是,如果我執行相同的查詢我-SQL控制檯,它完美地執行,並給予O/P:

+--------------+--------+-----------+ 
| districtName | status | stateName | 
+--------------+--------+-----------+ 
| Hisar  | 0  | HARYANA | 
| Gurgaon  | 0  | HARYANA | 
| Ambala  | 0  | HARYANA | 
| Rohtak  | 0  | HARYANA | 
| Sirsa  | 0  | HARYANA | 
+--------------+--------+-----------+ 

我的Hibernate代碼:

try{ 
     hibernateSession = HibernateUtil.getSession(); 
     if(hibernateSession != null){ 
      Query query = hibernateSession.createQuery("select dist.districtName, dist.status, stat.stateName from sa_district_master dist inner join sa_state_master stat on dist.stateId = stat.stateId"); 
      List list = query.list(); 
      for (Object object : list) { 
       Object[] objArray = (Object[])object; 
       System.out.println("Obj : " + objArray[0]); 
      } 
     } 
    } catch (Exception e){ 
     e.printStackTrace(); 
    }finally { 
     hibernateSession.flush(); 
     hibernateSession.close(); 
    } 

我不明白爲什麼我得到的錯誤...

plzzz幫助...

(Please tell me if anything else you want)

+2

變化'hibernateSession.createQuery'到'hibernateSession。 createSQLQuery' –

回答

3

您的查詢似乎更像是一個標準的SQL查詢比一個HQL查詢。使用Session#createSQLQuery(String sqlQuery)方法創建一個像這樣的SQL查詢。

Query query = hibernateSession.createSQLQuery("select dist.districtName, dist.status, stat.stateName from sa_district_master dist inner join sa_state_master stat on dist.stateId = stat.stateId"); 
0

您使用的是SQL查詢,所以你需要爲MySQL創建一個SQLQuery

try{ 
    hibernateSession = HibernateUtil.getSession(); 
    if(hibernateSession != null){ 
     Query query = hibernateSession.createSQLQuery("select dist.districtName, dist.status, stat.stateName from sa_district_master dist inner join sa_state_master stat on dist.stateId = stat.stateId"); 
     List list = query.list(); 
     for (Object object : list) { 
      Object[] objArray = (Object[])object; 
      System.out.println("Obj : " + objArray[0]); 
     } 
    } 
} catch (Exception e){ 
    e.printStackTrace(); 
}finally { 
    hibernateSession.flush(); 
    hibernateSession.close(); 
} 
1
Query query = hibernateSession.createSQLQuery("select dist.districtName, dist.status, stat.stateName from sa_district_master dist inner join sa_state_master stat on dist.stateId = stat.stateId"); 

使用.createSQLQuery方法