達(dá)內(nèi)java視頻-hibernate day理解Hibernate中對(duì)象的狀態(tài)及3轉(zhuǎn)_第1頁(yè)
達(dá)內(nèi)java視頻-hibernate day理解Hibernate中對(duì)象的狀態(tài)及3轉(zhuǎn)_第2頁(yè)
達(dá)內(nèi)java視頻-hibernate day理解Hibernate中對(duì)象的狀態(tài)及3轉(zhuǎn)_第3頁(yè)
達(dá)內(nèi)java視頻-hibernate day理解Hibernate中對(duì)象的狀態(tài)及3轉(zhuǎn)_第4頁(yè)
達(dá)內(nèi)java視頻-hibernate day理解Hibernate中對(duì)象的狀態(tài)及3轉(zhuǎn)_第5頁(yè)
已閱讀5頁(yè),還剩62頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

知識(shí)點(diǎn)列123掌握ThreadLocal的使用方法4增刪改查025注 持久化對(duì)象和一級(jí)緩存機(jī)制 對(duì)象的三種狀態(tài) 一級(jí)緩存機(jī)制 【案例1】對(duì)象的3種狀態(tài)演示 Hibernate延遲加載機(jī)制 基本原理 實(shí)現(xiàn)原理 【案例2】延遲加載機(jī)制 【案例3】Struts2和Hibernate實(shí)現(xiàn)增刪改查(2) 【案例4】在項(xiàng)目中使用延遲加載機(jī)制 many-to-one關(guān)聯(lián)映射 數(shù)據(jù)表的關(guān)聯(lián) many-to-one many-to-one需求下的常見(jiàn)操作 【案例5】many-to-one 持久化對(duì)象和一級(jí)緩存機(jī)制對(duì)象的三種狀態(tài)EmpEmpe=newEmp();一級(jí)緩存機(jī)制一旦用戶對(duì)緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫(kù)中。13DROPDROPTABLEIFEXISTSt_foo;CREATETABLEt_foo(t_idint(11)NOT,PRIMARYKEY(t_id))<!DOCTYPEhibernate- DTD" <classname="Foo"<idname="id"type="java.lang.Integer"<propertyname="value"type="java.lang.String"column="t_value" <generatorpackagepackageimportorg.hibernate.Session;importpublicclassHibernateUtilsprivatestaticThreadLocal<Session>tl=newThreadLocal<Session>();privatestaticConfigurationconf;static{factory=}}}**publicstaticSessiongetSession()Sessionsession=tl.get();if(session==null)}return}*publicstaticvoid{Sessionsession=if(session!={);}}packageimportjava.util.Date;packageimportjava.util.Date;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.junit.Test;}}}importimportpublicclass{{Foofoo=newFoo();Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();如上所示,當(dāng)執(zhí)行了 session.save(foo);語(yǔ)句執(zhí)行后,Hibernateinsert mit()操作時(shí),會(huì)自勱調(diào)用session.flash() 所以,執(zhí)行了2次update操作新建getfoo1foo2Foofoo2=(Foo)session.get(Foo.class,FooFoofoo2=(Foo)session.get(Foo.class,Foofoo1=(Foo)session.get(Foo.class,一旦用戶對(duì)緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫(kù)中。(案例結(jié)束Hibernate延遲加載機(jī)制基本原理調(diào)用實(shí)現(xiàn)原理該子類(lèi)重寫(xiě)了getXXX方法,在該方法中觸發(fā)了對(duì)數(shù)據(jù)庫(kù)的。loaditeratorHibernateGBLIBSpring——具體使用ThreadLocal——其實(shí)是一個(gè)線程為KEY的Map,2】延遲加載機(jī)制而當(dāng)調(diào)用Foofoo=Foofoo=(Foo)session.load(Foo.class,Foo{publicStringgetValue(){return}}Java.class我們并沒(méi)有創(chuàng)建Foo$$EnhancerByCGLIB$$b3a0560c這個(gè)類(lèi),cglib-2.1.3.jar調(diào)用了如下所示,F(xiàn)oo$$EnhancerByCGLIB$$b3a0560ccglibFoofinal package增加packageimportimportimportimportorg.hibernate.Query;importorg.junit.Test;importimportpublicclass{publicvoidtestIterator()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromFoo");//List<Foo>fooList=Iterator<Foo>fooIt=//selectt_idfromwhile{Foofoo=//select*fromt_foowheret_id=?}}}先執(zhí)行selectt_idfromt_foo;語(yǔ)句,從數(shù)據(jù)庫(kù)中僅僅將id取出;(案例結(jié)束參考請(qǐng)新增****@parampublicvoiddelete(Integer[]{Sessionsession=Transactiontx=for(Integerid:projects)Projectproject=newProject();}}packagepackageimportpublicclassProjectDeleteActionextendsBaseActionprivateProjectDaoprojectDao=new//privateInteger[]//OutputpublicStringexecute()throws{return}}}publicInteger[]getProjects()return}publicvoidsetProjects(Integer[]projects)jects=}<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN" <actionname="list"<result<action<result<actionname="create"<result<action<%@tagliburi="/struts-tags"<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN" <linkhref="../css/style.css"type="text/css"rel="stylesheet"<script<body<divcellPadding="0"width="100%"<br<tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<td <td<td<td<td<s:iterator

<td<inputtype="checkbox"<tdclass="altbg2"href="load.action?project.id=${id}"><b><s:propertyvalue="no"/></b></a><td<b><s:propertyvalue="name"/><td<b><s:propertyvalue="startDate"/><td<b><s:propertyvalue="endDate"/><br測(cè)試(略findById**@parampublicProjectfindById(Integer{Sessionsession=Projectproject=(Project)session.get(Project.class,id);return}ject.po.Project;publicclassProjectLoadActionextends{privateProjectDaoprojectDao=new////privateProjectpublicStringexecute()throwsExceptionreturn"success";}{return}{ject=}}project.struts.xml增加<actionname="load"><<actionname="load"<result<linkhref="css/style.css"type="text/css"rel="stylesheet"<body<div<tableclass="guide"cellSpacing="0"cellPadding="0"width="100%"<br<formid=""action=""value=""/><tableclass="tableborder"cellSpacing="0"<tr<tdclass="altbg1"<tdclass="altbg2"valign="middle"<inputtype="text"name="no"<spanstyle="color:red"><tdclass="altbg1"<td<inputtype="text"name="name"<spanstyle="color:red"><tdclass="altbg1"<td<inputtype="text"name="startDate"<spanstyle="color:<tdclass="altbg1"<td<inputtype="text"name="endDate"<spanstyle="color:<br<inputclass="buttontype="button"value="取消"name="settingsubmit"/><inputclass="buttontype="button"value="恢復(fù)"name="settingsubmit"/><inputclass="button"type="submit"value=name="settingsubmit"/><tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<td <td<td<td<td<s:iterator

<td<inputtype="checkbox"<tdclass="altbg2"<a<b><s:propertyvalue="no"/><td<b><s:propertyvalue="name"/><<td<b><s:propertyvalue="startDate"/><td<b><s:propertyvalue="endDate"/><br測(cè)跳轉(zhuǎn)到數(shù)據(jù)更新頁(yè)新增****@parampublicvoidupdate(Project{Sessionsession=openSession();}ject.po.Project;publicpublicclassProjectUpdateActionextends{privateProjectDaoprojectDao=new//privateProject//OutputpublicStringexecute()throws{return}{return}{ject=}}<actionname<actionname="update"<%@tagliburi="/struts-tags"<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN" <linkhref="../css/style.css"type="text/css"rel="stylesheet"<body<divcellPadding="0"width="100%"<br<s:formid="settings"action="update"method="post"<s:hidden<tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<tdclass="altbg1"<tdclass="altbg2"valign="middle"<s:textfieldname="project.no"<spanstyle="color:red"><tdclass="altbg1"<td<spanstyle="color:red"><tdclass="altbg1"<td<s:textfield<spanstyle="color:<tdclass="altbg1"<td<s:textfield<spanstyle="color:<brname="settingsubmit"/>name="settingsubmit"/><inputclass="button"type="submitvalue=name="settingsubmit"/>測(cè)試(略(案例結(jié)束4】在項(xiàng)目中使用延遲加載機(jī)制請(qǐng)Struts+Hibernate的jar 修改如果在ProjectDao中調(diào)用session.close()關(guān)閉了session,如果ProjectDao中丌調(diào)用session.close方法,這樣就造成一個(gè)如何在項(xiàng)目中使用延緩加載?OpesoIewpackagepackagepublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactorypublicstaticSomegetSome()returnnew}}f(s2mian(s1首先注意一點(diǎn),f()方法和main()方法同樣都是在一個(gè)線程中,有相同的線程號(hào)。的Some對(duì)象是同一個(gè)。packagepackageimportimportpublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactoryprivatestaticMap<Long,Some>map=newHashMap<Long,publicstaticSomegetSome()Somesome=if(some==some=new}}}}return假設(shè)Some就是Session,如果使用這種機(jī)制獲取Session,當(dāng)同一個(gè)用戶瀏覽器丌論怎么調(diào)用session都是同一個(gè)(叧要在相同的線程中)。packagepackagepublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=Somes2=SomeFactory.getSome();}}classSome{classSomeFactory//privatestaticMap<Long,Some>map=newHashMap<Long,privatestaticThreadLocal<Some>tl=newpublicstaticSomegetSome()//longthreadId=//Somesome=map.get(threadId);Somesome=tl.get();if(some==null)some=new//map.put(threadId,}return}}測(cè)試(略1(推薦packageimportpackageimportimportimportimportpublicclassHibUtilprivatestaticConfigurationprivatestaticSessionFactoryconf=new}publicstaticSessionreturn}}packagepackageimportorg.hibernate.Session;importpublicclassHibernateUtilsprivatestaticThreadLocal<Session>tl=newThreadLocal<Session>();privatestaticConfigurationconf;privatestaticSessionFactorystaticfactory=}publicstaticSessiongetSession()}}Sessionsession=tl.get();if(session==null)}return}publicstaticvoid{Sessionsession=tl.get();if(session!=null){);}}在器中調(diào)用closeSession()關(guān)閉Session。"-//Hibernate/HibernateConfigurationDTD3.0//EN"<propertyname="connection.url"><property<property<!--<property<property <propertypackagepackageimportimportimportpublicclassHibernateUtilsprivatestaticConfigurationprivatestaticSessionFactorystaticconf=newfactory=}**}}publicstaticSessiongetSession()return}*publicstaticvoidcloseSession()Sessionsession=if(session!={}}(案例結(jié)束many-to-one關(guān)聯(lián)映射數(shù)據(jù)表的關(guān)聯(lián)數(shù)據(jù)表的關(guān)聯(lián)丌一定導(dǎo)致Hibernate關(guān)聯(lián)映射。t_dept(t_id)的時(shí)候步驟1privateDept}<many-to-onename="dept"class="Dept"many-to-one需求下的常見(jiàn)操作1)保存2)取出Emp(帶著Dept)Empemp=<many-to-one...Dept還是用單獨(dú)的SQLfetchjoinSQL3)查詢(xún)Emp(帶著Dept)HQL:fromDept還是用單獨(dú)的HQLfromEmpeleftouterjoinfetche.dept4)根據(jù)特定條件查詢(xún)Emp(帶著Dept)HQL:fromEmpewhereHQLfromE='...'5)根據(jù)Dept的屬性查詢(xún)Emp(帶著Dept)HQL:fromEmpewhereHQL:fromEmpeleftouterjoinfetche.deptwheret_emp注意:這里丌用寫(xiě)主外鍵關(guān)聯(lián)約束也是可以的(現(xiàn)在約束的使用越來(lái)越少PRIMARYKEYt_loct_locvarchar(200)NOTPRIMARYKEY)insertintot_dept(t_name,t_loc))DROPTABLEIFEXISTSCREATETABLEt_deptDROPTABLEIFEXISTSCREATETABLEt_empt_idint(11)NOTNULLt_namevarchar(50)NOTNULL,t_salarydouble(9,2)NOTNULL,t_hire_datedateNOTNULL,t_registerchar(1)NOTt_idint(11)NOTNULLt_dept_idint(11)NOTEmpDeptEmp EmpsDeptDept DeptEmpsItem OrderItem packagepackageimportpublicclass{privateIntegerid;privateStringname;privatedoublesalary;privateDatehireDate;privateDatelastLogin;publicIntegergetDeptId(){returnpublicvoidsetDeptId(IntegerdeptId)this.deptId=publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicdoublegetSalary(){returnpublicvoidsetSalary(doublesalary){this.salary=publicDategetHireDate(){returnpublicvoidsetHireDate(DatehireDate)this.hireDate=publicDategetLastLogin(){returnpublicvoidsetLastLogin(DatelastLogin)this.lastLogin=publicbooleanisRegister(){returnpublicvoidsetRegister(booleanregister)this.register=}privateInteger<!DOCTYPEhibernate- DTD" <classname="Emp"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="salary"type="double"column="t_salary"<propertyname="hireDate"type="date"column="t_hire_date" packagepackagepublicclass{privateIntegerid;privateStringname;publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicStringgetLocation(){returnpublicvoidsetLocation(Stringlocation)}}}this.location=<!DOCTYPEhibernate- DTD" <classname="Dept"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="location"type="string"column="t_loc" many-to-onemany-to-onepackageimportpublicclass{privateIntegerid;privateStringname;privatedoublesalary;privateDatehireDate;privateDatelastLogin;privateDeptprivateDeptpublicpublicvoidsetDept(Deptdept){this.dept=publicDeptgetDept(){returnpublicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicdoublegetSalary(){returnpublicvoidsetSalary(doublesalary){this.salary=publicDategetHireDate(){returnpublicvoidsetHireDate(DatehireDate)this.hireDate=publicDategetLastLogin(){returnpublicvoidsetLastLogin(DatelastLogin)this.lastLogin=publicbooleanisRegister(){returnpublicvoidsetRegister(booleanregister)this.register=}<!DOCTYPEhibernate- "

DTD <classname="Emp"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="salary"type="double"column="t_salary"<propertyname="hireDate"type="date"column="t_hire_date"<many-to-onename="dept"class="Dept"<many-to-onename="dept"class="Dept"column="t_dept_id" 提供的<方方配置文件"-//Hibernate/HibernateConfigurationDTD <propertyname="connection.url"><property<propertyHibernate<!--<property<property<property insertinsertintot_dept(t_name,t_loc)packagepackagepublicclass{publicvoidtestSaveEmp()Empemp=newEmp();emp.setHireDate(newDate());Deptdept=newDept();Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();}}packagepackageimportimportimportimportpublicclass{publicvoidtestGetEmp()Sessionsession=HibernateUtils.getSession();Empemp=(Emp)session.get(Emp.class,1);}}}Hibernate:selectdept0_.t_idast1_1_0_,dept0_.t_nameasHibernate:selectdept0_.t_idast1_1_0_,dept0_.t_nameast2_1_0_,dept0_.t_locast3_1_0_fromt_deptdept0_wheredept0_.t_id=?Hibernate:selectemp0_.t_idast1_0_0_,emp0_.t_nameast2_0_0_,emp0_.t_salaryast3_0_0_,emp0_.t_hire_dateast4_0_0_,emp0_.t_last_loginast5_0_0_,emp0_.t_registerast6_0_0_,emp0_.t_dept_idast7_0_0_fromt_empemp0_,fromt_empejoint_dept即使我們使用的是get方法獲取的Emp對(duì)象EmpEmpemp=(Emp)session.get(Emp.class,<many-to-onename="dept"class="Dept"讓我們把兩條SQL合并為一條<many-to-onename="dept"class="Dept"lazy="false"<<many-to-onename="dept"class="Dept"戒<many-to-onename="dept"class="Dept"lazy="proxy"<many-to-onename="dept"class="Dept"lazy="false"Hibernate:Hibernate:selectemp0_.t_idast1_0_1_,emp0_.t_nameast2_0_1_,emp0_.t_salaryast3_0_1_,emp0_

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論