有5张表: 
A表:id,bid 
B表:id,cid 
C表:id 
D表:id,cid 
E表:id,did B、D是C的子表,A是B的子表,E是D的子表。 
全部左连接,要求顺序:A-B-C-D-E  可以反着:E-D-C-B-A,没什么区别,但是不能乱只能这两种。 
HQL语句怎么写?我自己写的第一条: 
      from A a left join B b on a.bid = b.id 
              left join C c on b.cid = c.id 
              left join D d on d.cid = c.id 
              left join E e on e.did = d.id 这条语句提示on关键字有错。 第二条: 
      from A a left join B b 
              left join C c 
              left join D d 
              left join E e 
          where a.bid = b.id and b.cid = c.id and d.cid = c.id and e.did = d.id 错误: 
org.springframework.orm.hibernate3.HibernateQueryException: Path expected for join! [ from com.newer.business.pojo.MProcedureModule mpme left join MProcedure mpe  left join MManufacture mme left join MProceduring mpg left join MProcedureModuling mpmg  where mpme.MProcedure.id = mpe.id and mpe.MManufacture.id = mme.id  and mpg.MManufacture.id = mme.id and mpmg.MProceduring.id = mpg.id and mme.id = 1]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [ from com.newer.business.pojo.MProcedureModule mpme left join MProcedure mpe  left join MManufacture mme left join MProceduring mpg left join MProcedureModuling mpmg  where mpme.MProcedure.id = mpe.id and mpe.MManufacture.id = mme.id  and mpg.MManufacture.id = mme.id and mpmg.MProceduring.id = mpg.id and mme.id = 1] 
Caused by: 
org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [ from com.newer.business.pojo.MProcedureModule mpme left join MProcedure mpe  left join MManufacture mme left join MProceduring mpg left join MProcedureModuling mpmg  where mpme.MProcedure.id = mpe.id and mpe.MManufacture.id = mme.id  and mpg.MManufacture.id = mme.id and mpmg.MProceduring.id = mpg.id and mme.id = 1] 没办法了,自己想不出,只能来请各位看看指点指点我。谢谢!!! 

解决方案 »

  1.   

    多表查询我还是建议还是用原生态的SQL语名。
      

  2.   

    sql写主外键关联,将对应的hbm.xml的延迟加载改成laze=false
      

  3.   

    重点就是要求使用Hibernate
    因为取的结果比较特殊的原因,延迟加载解决不了。
      

  4.   

    from A a left join B b on a.bid = b.id  
    ......
    前面加上selecct 查询字段 from A a left join B b on a.bid = b.id ...try it...
      

  5.   

    貌似Hibernate没有Join On的写法吧。
      

  6.   

    楼主你在搞什么啊? HQL写得都跟SQL都差不多了。 HQL查询的是对象!不是表!实体类之间到底是什么映射关系,搞清楚先。 
      

  7.   

    我知道,重点是,纯HQL现在整不出来,只能混搭,本来直接纯SQL我可以解决了,但是,一定要用到Hibernate,没办法,纯HQL因为实体间关联关系的问题,不能这么联,所以,才混搭,不过,现在解决了。谢谢大家帮忙哈。
      

  8.   

    hsql 是查询对象。他不只是on 关键字。它是根据entity配置的之间关系还组合on的条件
    from a left join a.b
    left join a.b.c
    left join a.b.c.d
    left join a.b.c.e要不你就用native sql。
      

  9.   

    写得优雅点就用criteria接口。否则就用hql吧。criteria.createAlias(basePropName, basePropName, Criteria.LEFT_JOIN); 看接口方法,很丰富的,根本上SQL语句能写的,都能实现。
      

  10.   

    既然你用hibernate,为什么还用HQL去写多表查询啊,用表级联不就行吗?
      

  11.   

    1.先在数据库中设置表与表之间设置外键
    2.然后写下面的这条语句
    from A a left join B b  
      left join C c 
      left join D d 
      left join E e