我在使用hibernate时遇到了一些麻烦。我正在尝试创建一个查询来返回咨询医疗病房的数量,我只是在pgAdmin上运行sql并且一切都很顺利,但是当我试图在hibernate上创建相同的sql时,这会返回一些错误。这是代码。
SQL:
select a.cod_ala ,count(cod_consulta) as max from Consulta as c
inner join Medico as m on m.cod_medico = c.cod_medico
inner join Ala as a on a.cod_ala = m.cod_ala
group by a.cod_ala
爪哇:
public void getConsultasAla() {
String sql = "select a.cod_ala, count(cod_consulta) as max from Consulta as c"
+" inner join Medico as m on m.cod_medico = c.cod_medico"
+" inner join Ala as a on a.cod_ala = m.cod_ala"
+" group by a.cod_ala";
Query query;
System.out.println(sql);
this.session = GeneralController.getSession();
query = this.session.createQuery(sql);
List list = query.list();
int i = 0;
while(list.iterator().hasNext()) {
System.out.println(list.get(i++));
}
}
错误:
jul 01, 2015 8:43:14 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:149: unexpected token: on
jul 01, 2015 8:43:14 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:149: unexpected token: on
line 1:149: unexpected token: on
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1694)
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1349)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1055)
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:701)
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:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.control.ConsultsController.getConsultasAla(ConsultsController.java:473)
at org.menu.MenuConsulta.MenuConsultas(MenuConsulta.java:97)
at org.hibernate.MainApp.menuPrincipal(MainApp.java:80)
at org.hibernate.MainApp.main(MainApp.java:145)
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: on near line 1, column 98 [select a.cod_ala, count(cod_consulta) as max from org.model.Consulta as c inner join Medico as m on m.cod_medico = c.cod_medico inner join Ala as a on a.cod_ala = m.cod_ala group by a.cod_ala]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:276)
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:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.control.ConsultsController.getConsultasAla(ConsultsController.java:473)
at org.menu.MenuConsulta.MenuConsultas(MenuConsulta.java:97)
at org.hibernate.MainApp.menuPrincipal(MainApp.java:80)
at org.hibernate.MainApp.main(MainApp.java:145)
PS:抱歉语法不好,英语是我的第二语言
答案 0 :(得分:0)
hibernate中没有“on”内连接;你使用实体而不是hibernate.joins中的实际表只能在关联实体之间完成
示例:
GMSPlacesClient
on子句是不必要的,因为Hibernate知道关联的映射(即OneToOne / ManyToOne等)。