One one = (One)session.get(One.class,new Integer(1)); Iterator iterone = one.getTwos().iterator(); while(iterone.hasNext()){ Two two = (Two) iterone.next(); Iterator itertwo = two.getThrees().iterator(); while(itertwo.hasNext()){ Three three = (Three) itertwo.next(); three.getFours().size(); } }
榪欐牱鎴戝湪Session鍏抽棴鍚庤繑鍥炵殑One閲屾槸浠嶰ne鍒癋our鐨勪俊鎭兘鏈夌殑銆?BR>鐒惰岃繖鏍峰仛鎵瀵艱嚧鐨勭粨鏋滄槸鐢熸垚澶ч噺鐨凷QL鏌ヨ錛岃繖鏄竴涓吀鍨嬬殑n+1 Selects闂銆傚鏋滅郴緇熺粨鏋勫眰嬈″錛岀鍚堟潯浠剁殑璁板綍澶氾紝閭d箞Hibernate涓轟綘鐢熸垚鐨凷QL鏌ヨ灝嗘槸闅句互鎺ュ彈鐨勩?BR>瀵逛簬榪欎釜渚嬪瓙鐢熸垚鐨凷QL鏄繖鏍風殑 Hibernate: select one0_.c_one_id as c1_0_, one0_.c_one_text as c2_3_0_ from One one0_ where one0_.c_one_id=? Hibernate: select twos0_.c_one_id as c2_1_, twos0_.c_two_id as c1_1_, twos0_.c_two_id as c1_0_, twos0_.c_one_id as c2_2_0_, twos0_.c_two_text as c3_2_0_ from Two twos0_ where twos0_.c_one_id=? Hibernate: select threes0_.c_two_id as c2_1_, threes0_.c_three_id as c1_1_, threes0_.c_three_id as c1_0_, threes0_.c_two_id as c2_1_0_, threes0_.c_three_text as c3_1_0_ from Three threes0_ where threes0_.c_two_id=? Hibernate: select fours0_.c_three_id as c2_1_, fours0_.c_four_id as c1_1_, fours0_.c_four_id as c1_0_, fours0_.c_three_id as c2_0_0_, fours0_.c_four_text as c3_0_0_ from Four fours0_ where fours0_.c_three_id=? Hibernate: select fours0_.c_three_id as c2_1_, fours0_.c_four_id as c1_1_, fours0_.c_four_id as c1_0_, fours0_.c_three_id as c2_0_0_, fours0_.c_four_text as c3_0_0_ from Four fours0_ where fours0_.c_three_id=? Hibernate: select threes0_.c_two_id as c2_1_, threes0_.c_three_id as c1_1_, threes0_.c_three_id as c1_0_, threes0_.c_two_id as c2_1_0_, threes0_.c_three_text as c3_1_0_ from Three threes0_ where threes0_.c_two_id=? Hibernate: select fours0_.c_three_id as c2_1_, fours0_.c_four_id as c1_1_, fours0_.c_four_id as c1_0_, fours0_.c_three_id as c2_0_0_, fours0_.c_four_text as c3_0_0_ from Four fours0_ where fours0_.c_three_id=? Hibernate: select fours0_.c_three_id as c2_1_, fours0_.c_four_id as c1_1_, fours0_.c_four_id as c1_0_, fours0_.c_three_id as c2_0_0_, fours0_.c_four_text as c3_0_0_ from Four fours0_ where fours0_.c_three_id=? 瀵逛簬榪欐牱鐨勯棶棰橈紝鍦ㄦ病鏈塇ibernate浠ュ墠鎴戜滑涓鑸兘鐢╦dbc鏉ュ仛錛岄偅鏍風殑璇濇垜浠叾瀹炵敤涓涓繘琛?嬈oin鐨剆ql璇彞灝卞彲浠ュ疄鐜幫紝浣嗘槸榪欐牱瑙e喅涔熸湁闂錛屽氨鏄繑鍥炵殑ResultSet涓殑鏁版嵁闈炲父澶氾紝鑰屼笖鏉備貢錛屽叾瀹炴槸浠巓ne鍒癴our騫寵鎺掑垪鐨勩傚浜庤繖鏍風殑緇撴灉闆嗘垜浠鎶婂畠鎵嬪姩褰卞皠鏇懼璞$粨鏋勪篃鏄竴涓緢澶嶆潅鐨勬搷浣溿?BR>騫稿ソHibernate3鍙互涓烘垜浠仛榪欎簺浜嬫儏(鎴戝啀涓嬈¤Hibernate鐨勫己澶ф墍闇囨捈)銆?BR>涓婇潰鐨勫疄鐜板彲浠ョ敤Criteria鏉ュ疄鐜幫細
榪欓噷鐨勯噸鐐規槸榪欏彞璇漜riteria.setFetchMode("twos",FetchMode.JOIN).setFetchMode("twos.threes",FetchMode.JOIN).setFetchMode("twos.threes.fours",FetchMode.JOIN).uniqueResult(); 鍦ㄧ敤Criteria涔嬪墠鍏堣緗瓼etchMode錛屽簲涓篊riteria鏄姩鎬佺敓鎴恠ql璇彞鐨勶紝鎵浠ョ敓鎴愮殑sql灝辨槸涓灞傚眰Join涓嬪幓鐨勩?BR>setFetchMode錛圫tring錛孧ode錛夌涓涓弬鏁版槸association path錛岀敤"."鏉ヨ〃紺鴻礬寰勩傝繖涓鐐瑰叿浣撶殑渚嬪瓙寰堝皯錛屾枃妗d篃娌℃湁鍐欐竻妤氥傛垜涔熸槸璇曚簡寰堜箙鎵嶈瘯鍑烘潵鐨勩?BR>灝辮繖涓緥瀛愭潵鎵鎶婂洜涓哄彇閬撶鍥涘眰錛屾墍浠ヨ榪涜涓夋setFetchMode 絎竴嬈$殑璺緞鏄痶wos錛屼竴浣峯ne涓湁two鐨凷et銆傝繖涓叿浣撹鏇村叿hbm.xml鐨勯厤緗潵瀹氥?BR>絎簩涓礬寰勫氨鏄痶wos.threes 絎笁涓氨鏄痶wos.threes.fours 涓嬈$被鎺紝涓灞傚眰澧炲姞鐨勩?BR>榪欐牱鍋氭硶鏈緇堢敓鎴愮殑SQL鏄繖鏍風殑錛?BR>Hibernate: select this_.c_one_id as c1_3_, this_.c_one_text as c2_3_3_, twos2_.c_one_id as c2_5_, twos2_.c_two_id as c1_5_, twos2_.c_two_id as c1_0_, twos2_.c_one_id as c2_2_0_, twos2_.c_two_text as c3_2_0_, threes3_.c_two_id as c2_6_, threes3_.c_three_id as c1_6_, threes3_.c_three_id as c1_1_, threes3_.c_two_id as c2_1_1_, threes3_.c_three_text as c3_1_1_, fours4_.c_three_id as c2_7_, fours4_.c_four_id as c1_7_, fours4_.c_four_id as c1_2_, fours4_.c_three_id as c2_0_2_, fours4_.c_four_text as c3_0_2_ from One this_ left outer join Two twos2_ on this_.c_one_id=twos2_.c_one_id left outer join Three threes3_ on twos2_.c_two_id=threes3_.c_two_id left outer join Four fours4_ on threes3_.c_three_id=fours4_.c_three_id where this_.c_one_id=? 铏界劧寰堥暱浣嗘槸鍙湁涓鏉QL璇彞銆傛ц兘瑕佸ソ寰堝銆侶ibernate鐨勫己澶т箣澶勬槸瀹冧細鎶婅繑鍥炵殑ResultSet鑷姩褰卞皠鍒頒綘鐨勫璞℃ā鍨嬮噷闈㈠幓銆傝繖灝變負鎴戜滑鐪佷簡寰堝浜嬨?BR> 鐪嬫潵Hibernate鐪熸槸涓涓愪漢瀵誨懗鐨凢ramework鍟娿?BR> 婧愮爜錛屾病浠涔堜笢瑗褲?BR>http://m.tkk7.com/Files/mstar/HiberFetch2.rar
濡傛灉浣犳妸HQL鍐欐垚榪欎釜鏍峰瓙錛?BR>update Contact as c set c.relationShip.relationId =1 ...... 鍙嶈屼細鍑洪敊錛屼篃灝辨槸璇達紝浣犲氨鎸夌収native SQL鍘誨啓灝辮浜嗐?BR>榪樻湁錛屽鏋滆兘浣跨敤榪欐牱鐨勫姛鑳?BR>hibernate.query.factory_class = org.hibernate.hql.classic.ClassicQueryTranslatorFactory 榪欎釜Hibernate閰嶇疆灞炴ц涓嶅幓鎺夛紝瑕佷笉鎹㈡垚org.hibernate.hql.ast.ASTQueryTranslatorFactory 涓婇潰閭d釜鏄敮鎸丠ibernate2鐨勩?BR>浣嗘槸鏈変竴鐐硅娉ㄦ剰錛屽鏋滀綘鐢╱pdate璇彞鏉ュ仛鐨勮瘽錛屽彲鑳戒駭鐢熺紦瀛樺悓姝ラ棶棰橈紝鑰屼笖涓ょ鏂規硶鏈鍚嶩ibernate鎵ц鐨勮鍙ュ樊涓嶅錛屽浜庣涓縐嶆柟娉旽ibernate涔熸槸灝辯敓鎴愪竴鏉pdate璇彞錛屽綋鐒跺洜涓烘垜鏄牴鎹富閿潵update鐨勶紝濡傛灉鎵歸噺澶勭悊鐨勮瘽榪樻槸鐢║pdate HQL蹇?BR>
]]>Could not initialize proxy - the owning Session was closedhttp://m.tkk7.com/mstar/archive/2005/09/05/12125.html榛戠伒榛戠伒Mon, 05 Sep 2005 15:57:00 GMThttp://m.tkk7.com/mstar/archive/2005/09/05/12125.htmlhttp://m.tkk7.com/mstar/comments/12125.htmlhttp://m.tkk7.com/mstar/archive/2005/09/05/12125.html#Feedback1http://m.tkk7.com/mstar/comments/commentRss/12125.htmlhttp://m.tkk7.com/mstar/services/trackbacks/12125.htmlHibernate綾葷殑initialize()闈欐佹柟娉曠敤浜庡湪Session鑼冨洿鍐呮樉寮忓垵濮嬪寲浠g悊綾誨疄渚嬶紝isInitialized()鏂規硶鐢ㄤ簬鍒ゆ柇浠g悊綾誨疄渚嬫槸鍚﹀凡緇忚鍒濆鍖栥備緥濡傦細