Path expected for join!错误处理

在Hibernate中,进行连接查询时,如果使用join语句,就容易产生标题所示的错误。

比如from Content o left join Mcp mcp where o.mcp.id=mcp.id and mcp.id=2

正确的做法应该是在A中建立private Mcp mcp,并映射,

然后通过from Content o left join o.mcp mcp where mcp.id=2

奇怪吧,这么罗嗦。其实大可不必这样连接操作,只要from Content where mcp is null or mcp.id=2即可


如果在Content对象中建立mcp(对象)字段的话,就用不了join。

这时只能用from Content o,Mcp mcp where o.mcpId is null or (o.mcpId=mcp.id and mcp.id=2)

这时的mcpId是一个与Mcp表中id对应的整型


结论: 尽量不用join,费力不讨好。




阅读更多

hibernate left outer join 出错 Path expected for join!

01-09

查询语句是:rnQuery query=session.createQuery("select s.studentName from Student as s left outer join StuResult as sr where s.studentId=8");rn报错:rnException in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [select s.studentName from gjs.pojo.Student as s left outer join StuResult as sr where s.studentId=8]rn at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:225)rn at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)rn at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:75)rn at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:54)rn at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)rn at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)rn at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)rn at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)rn at gjs.dao.Test.main(Test.java:62)rnCaused by: Path expected for join!rn at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:307)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3268)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3060)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)rn at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)rn at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)rn ... 9 morern很困惑,看了网上很多例子也这样写,不知道为什么我的就总报这错,希望高手指点。

没有更多推荐了,返回首页