有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]
没办法了,自己想不出,只能来请各位看看指点指点我。谢谢!!!
hibernate的左连接必须在配置文件中指定,如果想方便点些,可以直接用SQL本土语句。
HQL例子,这在配置文件中都指定了
from Cat as cat
inner join cat.mate as mate
left outer join cat.kittens as kitten
SQL方便点,session.CreateSQLQuery(sql)
我就直接用的SQL,而且连接这样写A.id (+)= B.id简单一点
你可以自己写完整的sql语句,调用的时候不调用hql方法,而是调用执行完整sql语句的方法试试呢。方法名字我有点记不清楚了... :D
是不是配置文件写的不对了?
你使用left join的话你的实体配置文件中有一个fetch属性,默认值为select,必须要改为join。
ls 正解,简单点就直接执行标准的SQL就行了
session.createSQLQuery(sql) 简单快捷
打开关联关系映射,你就可以直接from A a left join a.b b left join b.c c left join c.d d left join d.e e
不是关系都配置好了吗 在实体类中配置下抓取策略,想用join的话最好配置为eager
,然后直接用 join 属性.另一个对象 后面还可以加个别名。不用写on条件的
。