HQL鏄疕ibernate鐨勯噸瑕佺壒鑹詫紝瀵硅薄鏌ヨ璇█涓嶴QL澶氭湁涓嶅悓錛岃屽畼鏂瑰張娌℃湁鎻愪緵寰堣緇嗙殑鎵嬪唽錛屾墍浠ョ粡甯鎬細(xì)閬囧埌涓嶅悓鐨勯棶棰橈紝榪欓噷璁板綍涓嬫垜鑷繁緇忓巻榪囩殑HQL闂鍜屾煡鎵懼埌鐨勭瓟妗堛?BR>
娌℃湁澶栭敭鍏寵仈鐨勪袱涓〃鍋氳仈鍚堟煡璇?BR>
鏈変袱涓〃Zsplcxxb(嫻佺▼琛?鍜孡csplsb(嫻佺▼瀹℃壒鍘嗗彶琛?錛屼袱涓〃閮芥湁lcbh(嫻佺▼緙栧彿)錛屼絾鏄病鏈夎緗閿叧鑱旓紝鍋氫互涓嬬殑鏌ヨ錛?/P>
from Zsplcxxb as a inner join Lcsplsb as b on a.lczbh = b.lczbh and b.blrbh='' and b.blrbh != a.sqrbh and a.clrbh != '' order by a.zhgxrq desc
鍑虹幇浠ヤ笅闂錛堝湪HibernateConsole涓繍琛岋級(jí)
1 errors occurred while listing (and calling getPathNames).
net.sf.hibernate.QueryException: outer or full join must be followed by path expression [from com.aljoin.xzsp.entity.Zsplcxxb as a inner join Lcsplsb as b on a.lczbh = b.lczbh and b.blrbh='' and b.blrbh != a.sqrbh and a.clrbh != '' order by a.zhgxrq desc]
鍘熷洜錛?BR> 鍙湁涓や釜entity鏈塧ssociation鐨勭姸鍐典笅錛屾墠鑳戒嬌鐢╦oin銆傛瘮濡俍splcxxb and Lcsplsb鏈塵any-to-one鐨刟ssociation
鍙︼細(xì)HQL涓病鏈夆渙n a.lczbh = b.lczbh鈥濊繖鏍風(fēng)殑璇硶
瑙e喅鏂規(guī)硶錛?BR>錛戯紟鏀圭敤where瀛愬彞
from Zsplcxxb as a, Lcsplsb as b where a.lczbh = b.lczbh and b.blrbh=? and b.blrbh != a.sqrbh and a.clrbh != ? order by a.zhgxrq desc
錛掞紟濡傛灉鏄痩eft join or right join錛屽ソ鍍忓彧鑳界敤session.createSQLQuery() or NamedQuery鐩存帴鎵цNative SQL浜?jiǎn)銆?BR>
鏂伴棶棰?/STRONG>錛?BR>銆榪斿洖鐨凩ist涓瘡涓欏歸兘鏄竴涓璞℃暟緇勶紝鍖呭惈浜?jiǎn)Zsplcxxb and Lcsplsb涓や釜entity
鍘熷洜錛?/STRONG>
銆鈥渋nner join fetch鈥濆瓙鍙ワ紝鍒?Lcsplsb 璇誨彇鍚庣珛鍗沖~鍏呭埌 Zsplcxxb 涓?BR>銆鈥渋nner join鈥濆瓙鍙ワ紝鍒欒繑鍥炰袱涓猠ntity銆傝寃here瀛愬彞絳夊悓浜庘渋nner join鈥濆瓙鍙?BR>
瑙e喅鏂規(guī)硶錛氬姞涓奡elect璇彞
select a from Zsplcxxb as a, Lcsplsb as b where a.lczbh = b.lczbh and b.blrbh=? and b.blrbh != a.sqrbh and a.clrbh != ? order by a.zhgxrq desc
鏂伴棶棰?/STRONG>錛?BR>榪斿洖鐨刏splcxxb entity鏈夐噸澶嶇殑銆傚洜涓轟竴涓祦紼嬫湁澶氫釜瀹℃壒璁板綍錛宨nner join 涔嬪悗瀵瑰簲姣忎釜瀹℃壒璁板綍閮戒細(xì)榪斿洖涓鏍風(fēng)殑嫻佺▼銆?BR>
瑙e喅鏂規(guī)硶錛氬姞涓奷istinct璇彞
select distinct a from Zsplcxxb as a, Lcsplsb as b where a.lczbh = b.lczbh and b.blrbh=? and b.blrbh != a.sqrbh and a.clrbh != ? order by a.zhgxrq desc
鏂伴棶棰?/STRONG>錛?BR>
net.sf.hibernate.JDBCException: Could not execute query
java.sql.SQLException: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
鍘熷洜錛?BR>濡傛灉鏄疭QL鐨勮瘽錛屼嬌鐢?distinct a.* 鍗沖彲銆備絾鏄浜嶩QL錛屾病娉曚嬌鐢?* 鏉ラ夋嫨灞炴э紝浜庢槸Hibernate灝辮涓篴.zhgxrq娌℃湁鍑虹幇鍦╯elect 瀛愬彞涓?BR>
瑙e喅鏂規(guī)硶錛?BR>浣跨敤 order by entity.pk or order by entity 銆傦紙鐪熸槸濂囨紝鑳芥壘鍒頒富閿紝鍏朵粬灞炴т負(fù)浠涔堟壘涓嶅埌錛燂級(jí)
select distinct a from Zsplcxxb as a, Lcsplsb as b where a.lczbh = b.lczbh and b.blrbh=? and b.blrbh != a.sqrbh and a.clrbh != ? order by a desc
鍙傝冩枃妗o細(xì)
錛戯紟javaworld@TW錛?A >[鍟忛]闂滄柤浣跨敤HQL left outer join
錛掞紟JavaEye錛?A class=maintitle >銆庡嚭閿欍忎袱涓病鏈夊閿叧鑱旂殑琛ㄥ仛inner join鐨勬椂鍊?HQL璇彞鍑洪敊
錛擄紟Hibernate瀹樻柟璁哄潧錛?A >Chapter 14. HQL: The Hibernate Query Language
]]>