我正在使用inner-join
执行hibernate-session.createQuery()
查询,我收到错误:
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
但是,如果我在my-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帮助...
(请告诉我你还有别的想法)
答案 0 :(得分: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");
答案 1 :(得分: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");
对mysql使用.createSQLQuery方法
答案 2 :(得分:0)
您正在使用SQL查询,因此您需要创建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();
}