日期:2014-05-17  浏览次数:20673 次

hibernate使用动态模型如何进行多表查询?
配置文件如下
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class entity-name="SF_ORGANIZATION" table="SF_ORGANIZATION">
  <id name="ID" type="string">
  <column name="ID" length="32"/>
  <generator class="assigned"/>
  </id>
  <property name="ORG_CODE" type="string">
  <column name="ORG_CODE" length="40"/>
  </property>
  <property name="ORG_NAME" type="string">
  <column name="ORG_NAME" length="100"/>
  </property>
  <property name="PARENT_ID" type="string">
  <column name="PARENT_ID" length="32"/>
  </property>
  <property name="CORP_ID" type="string">
  <column name="CORP_ID" length="32"/>
  </property>
  <property name="ORG_TYPE" type="string">
  <column name="ORG_TYPE" length="2"/>
  </property>
  <property name="TS" type="string">
  <column name="TS" length="19"/>
  </property>
  <property name="IS_USE" type="string">
  <column name="IS_USE" length="1"/>
  </property>

  <bag name="models"
  cascade="all"
  inverse="true">
  <key column="ORG_ID"/>
  <one-to-many class="SF_USER"/>
  </bag>
  </class>
  
</hibernate-mapping>

代码
String hql = "select new map(u.ID as ID) from SF_ORGANIZATION o right join SF_USER u where o.ID = u.ORG_ID";
List ls = commonDao.queryList(hql);
执行结果如下
2008-04-08 16:50:10,281 ERROR [org.hibernate.hql.PARSER] - Path expected for join!
2008-04-08 16:50:10,328 ERROR [org.hibernate.hql.PARSER] - Invalid path: 'u.ID'
2008-04-08 16:50:10,328 ERROR [org.hibernate.hql.PARSER] - <AST>:0:0: unexpected end of subtree
[16:50:10.328] org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
[16:50:10.328] Caused by: java.lang.NullPointerException
[16:50:10.328] at org.hibernate.hql.ast.HqlSqlWalker.setAlias(HqlSqlWalker.java:851)
[16:50:10.328] at org.hibernate.hql.antlr.HqlSqlBaseWalker.aliasedSelectExpr(HqlSqlBaseWalker.java:2069)
[16:50:10.328] at org.hibernate.hql.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2226)
[16:50:10.328] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1952)
[16:50:10.328] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
[16:50:10.328] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.