hibernate join关联查询问题
hibernate在没有配置对象关联关系的情况下可以使用left join 吗?注:数据库所有表均没有外键
select u.*,r.*,u1.username
from Users_Roles ur,User u left join User u1 on u1.id=u.operator ,Role r where 1=1 and ur.user_Id=u.id and ur.role_Id=r.id
这个语句在mysql下可以正常运行,但是使用hibernate的Query进行查询就会报错。
这是什么原因呢? --------------------编程问答-------------------- 报什么错,贴出来分析 --------------------编程问答--------------------
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.hibernate.hql.antlr.HqlSqlBaseWalker - select << begin [level=1, statement=select]
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.hibernate.hql.ast.tree.FromElement - FromClause{level=1} : cn.com.oims.pojo.User (u) -> user0_
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.hibernate.hql.ast.util.LiteralProcessor - setSQLValue() cn.com.oims.pojo.Role -> null
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.hibernate.hql.ast.tree.FromReferenceNode - Resolved : cn.com.oims.pojo.Role -> null
[DEBUG] [2013-12-17 14:32:12][http-bio-8080-exec-7] org.springframework.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:371)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3452)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3239)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
at org.hibernate.hql.ast.QueryTranslatorImpl.易做图yze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at cn.com.oims.dao.BaseDao.queryByPage(BaseDao.java:66)
at cn.com.oims.dao.impl.UserDaoImpl.getUsersByPage(UserDaoImpl.java:59)
at cn.com.oims.service.impl.UserServiceImpl.getUsersByPage(UserServiceImpl.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
hibernate使用join进行联合查询时,是否必须配置关联关系?如果不是,能给个实例吗? --------------------编程问答-------------------- 建议用hql,或者就是DAO。
如果在hibernate下用sql,有点搞笑。 --------------------编程问答-------------------- 在查询的时候,把表名换成数据库的表名,别用对象名试试。 --------------------编程问答--------------------
给出的sql是从hql修改来的,就是将类名换成了表明,将有些属性换成了数据库表字段。 --------------------编程问答--------------------
没换以前报错吗?
补充:Java , Java EE