]]>hibernate鍜宩dbc浜嬪姟緇熶竴鎺у埗http://m.tkk7.com/leekiang/archive/2009/12/11/305492.htmlleekiangleekiangThu, 10 Dec 2009 16:14:00 GMThttp://m.tkk7.com/leekiang/archive/2009/12/11/305492.htmlhttp://m.tkk7.com/leekiang/comments/305492.htmlhttp://m.tkk7.com/leekiang/archive/2009/12/11/305492.html#Feedback0http://m.tkk7.com/leekiang/comments/commentRss/305492.htmlhttp://m.tkk7.com/leekiang/services/trackbacks/305492.html
鍘熺悊灝辨槸淇濊瘉浜?connection 鐨勫敮涓鎬с?br />
jdbc鎴戞槸璋僺pring鐨刯dbcTemplate鏉ユ搷浣滐紝
緇忚繃嫻嬭瘯銆傚湪鍚屼竴涓暟鎹簮鐨勬儏鍐典笅鐩存帴浣跨敤Hibernate鐨凾xManager鍙互鍚屾浜嬪姟錛岄棶棰樿В鍐熾?br />
鈥?br />
Rod Johnson鐨勮瘽錛?
寮曠敤
It is possible--and sometimes useful--to have coordinated transactions
for both. Your JDBC transactions will be managed by the
HibernateTransactionManager if you work with the same JDBC DataSource
in the same transaction. That is, create the SessionFactory using
Spring's SessionFactoryBean using the same DataSource that your
JdbcTemplates use.
The only issue to watch, of course, is that you may be invalidating
your Hibernate cache by JDBC changes. Generally I find it best to use
JDBC to update only tables that don't have Hibernate mappings.
Juergen Hoeller鐨勮瘽錛?
寮曠敤
As Rod said, simply keep using HibernateTransactionManager, which
auto-detects the DataSource used by Hibernate and seamlessly exposes
Hibernate transactions as JDBC transactions for that DataSource. JDBC
code that accesses the same DataSource via Spring will automatically
participate in such transactions.
Note that you must specify the DataSource for Hibernate via
LocalSessionFactoryBean's "dataSource" property to allow
HibernateTransactionManager to auto-detect it. Alternatively, you can
explicitly pass the DataSource to HibernateTransactionManager's
"dataSource" property. http://www.fireflow.org/redirect.php?tid=498
Rod Johnson鍦╯pring 璁哄潧涓湁涓鍙ヨ瘽寰堝ソ鐨勬葷粨浜嗗浣曞湪嫻嬭瘯涓鐞唄ibernate緙撳瓨: Remember that you can clear the Hibernate session, removing objects already associated with it. This is often necessary before requerying in tests, and solves most (if not all) problems. I typically use JDBC for verification. The pattern is - do Hibernate operation - flush Hibernate session - issue JDBC query to verify results That way I'm verifying what Hibernate did to the database in the same transaction.
]]>hibernate寮傚父"Found shared references to a collection"http://m.tkk7.com/leekiang/archive/2008/10/31/237908.htmlleekiangleekiangFri, 31 Oct 2008 14:11:00 GMThttp://m.tkk7.com/leekiang/archive/2008/10/31/237908.htmlhttp://m.tkk7.com/leekiang/comments/237908.htmlhttp://m.tkk7.com/leekiang/archive/2008/10/31/237908.html#Feedback2http://m.tkk7.com/leekiang/comments/commentRss/237908.htmlhttp://m.tkk7.com/leekiang/services/trackbacks/237908.html鍋囧畾,Parent綾繪湁涓涓猄et灞炴э紝閲岄潰鏀劇殑鏄疭on銆傚鏋滄煡璇?from Parent",鏌愪釜Parent鍝曚竴涓猄on閮芥病鏈夛紝閭d釜Set灞炴т笉浼氫負null錛岃屾槸涓涓┖闆嗗悎銆?br />榪欐椂鍊欏鏋滀綘Parent newP=new Parent();鐒跺悗BeanUtils.copyPropertis(newP,origP);鏈鍚庡氨浼氭姤hibernate寮傚父"Found shared references to a collection"銆?br />娉?hibernate鍦ㄤ粈涔堟椂鏈哄彂鐜?涓や釜瀵硅薄鍏變韓涓涓泦鍚?鐨勬儏鍐電殑錛熸垜榪欒竟鐨勪緥瀛愭槸鍦ㄤ笅涓嬈℃煡璇㈡椂鍙戠幇鐨勩?br /> 鍘熷洜: BeanUtils.copyPropertis鏄祬鎷瘋礉錛屽鑷磋繖涓や釜瀵硅薄寮曠敤鐨凷et鏄悓涓涓猄et,榪欏湪hibernate涓槸涓嶅厑璁哥殑錛屽弬瑙丠ibernate reference絎?绔犵殑"Two entities may not share a reference to the same collection
instance"銆?br />榪欑闂甯歌浜庡鍒跺璞℃椂銆?br />濡備綍瑙e喅:newP.setSonSet(null); 榪樻湁浜鴻鍘熷洜鍙兘鏄茍鍙戞搷浣?http://m.tkk7.com/fastzch/archive/2006/12/22/89520.html
]]>hibernate鏉傝http://m.tkk7.com/leekiang/archive/2008/02/16/180148.htmlleekiangleekiangFri, 15 Feb 2008 21:04:00 GMThttp://m.tkk7.com/leekiang/archive/2008/02/16/180148.htmlhttp://m.tkk7.com/leekiang/comments/180148.htmlhttp://m.tkk7.com/leekiang/archive/2008/02/16/180148.html#Feedback0http://m.tkk7.com/leekiang/comments/commentRss/180148.htmlhttp://m.tkk7.com/leekiang/services/trackbacks/180148.htmlselect new Map(a.id as id,a.name as name,b.type as type) from A a,B b where a.id=b.aId; ... List mapList=query.list(); ....
select new MyObj(id,name,deptid,deptName) from tb_usr,tb_dept where ..... 榪欑鏂瑰紡瑕佹眰MyObj綾繪湁瀵瑰簲鐨勬瀯閫犳柟娉?br /> 2.HQL supports subqueries in the where clause. We can鈥檛 think of many good uses for subqueries in the from clause, although select clause subqueries might be a nice future extension. 涓嶆敮鎸乫rom鍚庣殑瀛愭煡璇紝鏀寔where瀛愭煡璇?br />http://blog.zol.com.cn/655/article_654256.html
3錛?String sql="select {fi.*} from FuncInfo fi " + 聽聽 "left join RoleSubFunc rsf on fi.FuncId=rsf.FuncId "+ 聽聽聽聽聽聽 "left join RoleInfo ri on rsf.RoleId=ri.RoleId "+ 聽聽聽聽聽聽 "left join UserRole ur on ri.RoleId=ur.RoleId "+ 聽聽聽聽聽聽 "where ur.UserId='"+userId+"'";//鍙互鏃犻檺鍚戜笂鎵劇埗綰?br />聽List list =session.createSQLQuery(sql).addEntity("fi", FuncInfo.class).list();
4,涓轟粈涔圚ibernate 3涓殑HQL鏃犳硶鏌ヨ姹夊瓧 浣跨敤鍚屾牱鐨勪唬鐮佸拰閰嶇疆鏂囦歡錛屽湪Hibernate 2涓婂畬鍏ㄦ病鏈夐棶棰橈紝鍦℉ibernate 3涓紝浣跨敤濡備笅HQL鏌ヨ錛屾棤娉曞緱鍒版紜殑緇撴灉闆嗭細 String hql = "from story where title like '%姹夊瓧%'"; Query q = session.createQuery(hql); 浣嗙敤涓嬮潰鐨凥QL鏌ヨ錛屽嵈鍙互寰楀埌姝g‘緇撴灉闆嗭細 String hql = " from story where title like '%english%'"; Query q = session.createQuery(hql); 絳旓細濡傛灉閲囩敤鐨勬槸鎷兼帴HQL鐨勬柟寮忥紝浠嶩ibernate 2鍗囩駭鍒癏ibernate 3紜疄浼氬嚭鐜版眽瀛椾貢鐮侀棶棰樸傚湪鎺у埗鍙頒腑鍙互鐪嬪埌錛孲QL鐨勬眽瀛楅儴鍒嗗彉鎴愪簡涔辯爜錛?br />[DEBUG] 2005-08-14 14:33:58 org.hibernate.SQL - "select story0_.content from story as story0_ where story0_.title like '%&鈥撀懊┾?amp;&艩簍&鈧? 鍦℉ibernate涓紝鏌ヨ鏃跺簲灝介噺浣跨敤鍗犱綅絎︾殑鍐欐硶錛堝涓嬶級錛岃繖鏍鋒棦鍙互閬垮厤涔辯爜闂錛屽張鍙互閬垮厤娼滃湪鐨凷QL娉ㄥ叆鏀誨嚮錛?br />getHibernate().find("from story where title like ? ", "%姹夊瓧%") 娉?鐢?update TbTest set name='寮犱笁' "涔熶細鏈夌被浼肩殑闂,瑕佹敼涓哄崰浣嶇鐨勫啓娉?br />http://xqfy1983.blog.sohu.com/61054398.html
5,浣跨敤Hibernate.DATE鏃訛紝寰楀埌鐨勬椂闂翠笉甯﹀垎縐掋?br />聽 鍙互鑰冭檻浣跨敤Hibernate.CALENDAR,瀹為檯綾誨瀷鏄痡ava.util.GregorianCalendar,鐒跺悗鍐峠etTime() 6,鎶ヨ〃鏌ヨ聽 select new Table(t1.a,t2.b ...)聽 from ..... 聽 select new Object(vo.id,vo.name) from VO vo 聽 榪欑鍐欐硶闇浜嬪厛鍐欏ソ鏋勯犳柟娉?br />7,List cats = sess.createSQLQuery("select {cat.*} from cats cat") 聽 聽 聽 聽 .addEntity("cat", Cat.class); 聽 聽 聽 聽 .setMaxResults(50); 聽 聽 聽 聽 .list(); 8,璨屼技hibernate娣誨姞鎴栦慨鏀瑰璞?濡傛灉瀛楃涓茬被鍨嬬殑灞炴х殑鍊間負絀哄瓧絎︿覆(澶у皬涓?),鍒欒嚜鍔ㄥ綋null澶勭悊 9,濡傛灉鍦╤bm涓煇灞炴ц涓簄ot null錛岀劧鍚庡鏋減o涓殑璇ュ睘鎬т負null錛屽垯hibernate浼氭姏寮傚父 聽 浣嗗鏋滃湪hbm涓緗睘鎬х殑闀垮害錛岃屽疄闄呯殑闀垮害瓚呰繃浜嗭紝hibernate涓嶄細鎶涘紓甯?br />10,濡傛灉瑕佷嬌鐢熸垚寰梥ql涓嶆槸姣忔閮藉寘鎷墍鏈夌殑鍒楋紝鍙厤緗弬鏁板疄鐜?
6錛宻etResultTransformer涓巃ddEntity鐨勪竴涓尯鍒槸鍓嶈呮敮鎸佹煡浠繪剰鐨勫垪錛屽悗鑰呭繀欏葷敤select * from users鐨勫艦寮忔垨select {a.*},{b.*} from a,b where ....銆?br />
7,Map vs. Object[]
Since you can also use a transformer that return a Map from alias to
value/entity (e.g. Transformers.ALIAS_TO_MAP), you are no longer
required to mess with index based Object arrays when working with a
result.
List iter = s.createQuery( "select e.student.name as studentName," + " e.course.description as courseDescription" + "from Enrolment as e") .setResultTransformer( Transformers.ALIAS_TO_MAP ) .iterate();
String name = (Map)(iter.next()).get("studentName");
Again, this works equally well for Criteria, HQL and native SQL.
]]>hibernate闂http://m.tkk7.com/leekiang/archive/2007/09/28/148919.htmlleekiangleekiangThu, 27 Sep 2007 19:51:00 GMThttp://m.tkk7.com/leekiang/archive/2007/09/28/148919.htmlhttp://m.tkk7.com/leekiang/comments/148919.htmlhttp://m.tkk7.com/leekiang/archive/2007/09/28/148919.html#Feedback0http://m.tkk7.com/leekiang/comments/commentRss/148919.htmlhttp://m.tkk7.com/leekiang/services/trackbacks/148919.html 2,鎵ц浠ヤ笅璇彞鏃秇ibernate3.0.5浼氭姤閿?鑰?.2涓嶄細 sql="select 1+1 from dual"; session.createSQLQuery(sql).uniqueResult() 鎶ラ敊:addScalar() or addEntity()聽must be called on a sql query before executing the query.
3,<many-to-one> 鐨刲azy璁劇疆涓簍rue鏃訛紝get瀛愬璞′笉浼氭妸璇ョ埗瀵硅薄鎶撹繃鏉?浣嗗彲浠ユ墜鍔ㄥ啓浠g爜鎶撳彇鐖跺璞?br />濡?Son son =(Son)this.getHibernateTemplate().get(Son.class, id); 聽聽聽 聽聽 鐒跺悗鎵ц son.getParent().getName(); 聽聽 榪欐牱涓嶄粎浠呬細鎶撳彇鍒皀ame,鍏朵粬鎵鏈夌殑灞炴уage,sex絳夐兘浼氬彇鍒幫紝鍗崇敤p.getParent().getAge()杈懼埌浜嗗悓鏍風殑鏁堟灉錛屽悗鍙伴兘鎵ц浜唖elect * from parent where id=?聽聽 娉ㄦ剰鐢╬.getParent()浠呰兘寰楀埌parent鐨刬d. 聽聽 榪欐椂debug鏌ョ湅parent瀵硅薄鐨勫唴瀛樺揩鐓?鐪嬪埌鐨勬槸涓涓敤cglib瀹炵幇鐨勪唬鐞嗗璞? Hibernate閫氳繃浣跨敤CGLIB,鏉ュ疄鐜板姩鎬佹瀯閫犱竴涓洰鏍囧璞$殑浠g悊綾誨璞★紝騫朵笖鍦ㄤ唬鐞嗙被瀵硅薄涓寘鍚洰鏍囧璞$殑鎵鏈夊睘鎬у拰鏂規硶錛岃屼笖鎵鏈夊睘鎬у潎琚祴鍊間負null銆傞氳繃璋冭瘯鍣ㄦ樉紺虹殑鍐呭瓨蹇収錛屾垜浠彲浠ョ湅鍑烘鏃剁湡姝g殑User瀵硅薄錛屾槸鍖呭惈鍦ㄤ唬鐞嗗璞$殑CGLIB$CALBACK_0.target灞炴т腑錛屽綋璋冪敤son.getName()鏂規硶錛岃繖鏃墮氳繃CGLIB璧嬩簣鐨勫洖璋冩満鍒訛紝瀹為檯涓婅皟鐢–GLIB$CALBACK_0.getName()鏂規硶錛屽綋璋冪敤璇ユ柟娉曟椂錛孒ibernate浼氶鍏堟鏌GLIB$CALBACK_0.target灞炴ф槸鍚︿負null錛屽鏋滀笉涓虹┖錛屽垯璋冪敤鐩爣瀵硅薄鐨刧etName鏂規硶錛屽鏋滀負絀猴紝鍒欎細鍙戣搗鏁版嵁搴撴煡璇紝鐢熸垚綾諱技榪欐牱鐨凷QL璇彞錛歴elect * from parent where id=鈥?鈥?鏉ユ煡璇㈡暟鎹紝騫舵瀯閫犵洰鏍囧璞★紝騫朵笖灝嗗畠璧嬪煎埌CGLIB$CALBACK_0.target灞炴т腑銆?br />榪欐牱錛岄氳繃涓涓腑闂翠唬鐞嗗璞★紝Hibernate瀹炵幇浜嗗疄浣撶殑寤惰繜鍔犺澆錛屽彧鏈夊綋鐢ㄦ埛鐪熸鍙戣搗鑾峰緱瀹炰綋瀵硅薄灞炴х殑鍔ㄤ綔鏃訛紝鎵嶇湡姝d細鍙戣搗鏁版嵁搴撴煡璇㈡搷浣溿?br />聽聽聽 <many-to-one> 鐨刲azy璁劇疆涓篺alse鏃?鎶撳彇鐖跺璞℃病鏈夐噰鐢ㄤ唬鐞嗘満鍒躲?br /> 4,鐢╯ession.close()錛屾墽琛屽嬈℃煡璇㈠悗鎶ession is closed鐨勯敊璇?鑰宧ibernateSessionFactory.closeSession()娌℃湁榪欎釜闂錛岃繖鏄粈涔堝師鍥?