锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲天堂视频在线观看,亚洲国产综合自在线另类,亚洲精品乱码久久久久久http://www.tkk7.com/jinfeng_wang/category/481.htmlG-G-S,D-D-U!zh-cnTue, 27 Feb 2007 10:06:06 GMTTue, 27 Feb 2007 10:06:06 GMT60Mapping collections of value types http://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3132.htmljinfeng_wangjinfeng_wangMon, 11 Apr 2005 06:12:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3132.htmlhttp://www.tkk7.com/jinfeng_wang/comments/3132.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3132.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/3132.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/3132.htmlMapping collections of value types (from 銆奾ibernate in action銆媍hapter 6)

 

榪欓噷鎵涓劇殑渚嬪瓙錛岄兘鏄熀浜庤繖鏍風殑涓涓儏鏅紝Item涓寘鍚湁Images銆?/SPAN>

<set name="images" lazy="true" table="ITEM_IMAGE">

<key column="ITEM_ID"/>

<element type="string" column="FILENAME" not-null="true"/>

</set>

榪欓噷鐨?/SPAN><key>瀹氫箟浜嗗閿?/SPAN>ITEM_ID錛?/SPAN><elememt>鍒欏0鏄庝簡闆嗗悎鐨勫唴閮ㄦ暟鎹厓绱犮?/SPAN><set>瀹氫箟鐨勯泦鍚堜腑涓嶅彲浠ユ湁閲嶅鐨勬暟鎹?/SPAN>

 

<idbag name="images" lazy="true" table="ITEM_IMAGE">

<collection-id type="long" column="ITEM_IMAGE_ID">

<generator class="sequence"/>

</collection-id>

<key column="ITEM_ID"/>

<element type="string" column="FILENAME" not-null="true"/>

</idbag>

ITEM_IMAGE_ID涓轟富閿紝鑰?/SPAN>ITEM_ID鍒欎負澶栭敭錛?/SPAN><element>鍏冪礌涓嶅彉錛岃繖灝卞厑璁擱泦鍚堜腑鐨勬暟鎹噸澶嶃?/SPAN>

 

<list name="images" lazy="true" table="ITEM_IMAGE">

<key column="ITEM_ID"/>

<index column="POSITION"/>

<element type="string" column="FILENAME" not-null="true"/>

</list>

榪欓噷涓?/SPAN>ITEM_IMAGE琛ㄦ坊鍔犱簡index錛屾暣涓〃鐨勪富閿槸ITEM_ID鍜?/SPAN>POSITION鏋勬垚鐨勫鍚堜富閿紝瀹冨厑璁擱泦鍚堜腑鐨勬暟鎹牴鎹?/SPAN>POSITION榪涜鎺掑簭銆?/SPAN>

 

<map name="images"

lazy="true"

table="ITEM_IMAGE"

sort="natural">

<key column="ITEM_ID"/>

<index column="IMAGE_NAME" type="string"/>

<element type="string" column="FILENAME" not-null="true"/>

</map>

榪欓噷涓?/SPAN>ITEM_IMAGE琛ㄦ坊鍔犱簡index錛屾暣涓〃鐨勪富閿槸ITEM_ID鍜?/SPAN>POSITION鏋勬垚鐨勫鍚堜富閿紝瀹冨厑璁擱泦鍚堜腑鐨勬暟鎹牴鎹?/SPAN>sort鎸囧畾鐨勫唴瀹硅繘琛屾帓搴忥紝榪欓噷鍏佽鐢ㄦ埛瀵?/SPAN>sort榪涜瀹氬埗銆?/SPAN>

 

<map name="images"

lazy="true"

table="ITEM_IMAGE"

order-by="IMAGE_NAME asc">

<key column="ITEM_ID"/>

<index column="IMAGE_NAME" type="string"/>

<element type="string" column="FILENAME" not-null="true"/>

</map>

榪欓噷閲囩敤鐨勬槸orbder-by錛屾敞鎰?/SPAN>sort鍜?/SPAN>order-by鐨勫尯鍒?/SPAN>Sort鏄湪鍐呭瓨涓嬌鐢?/SPAN>comparator榪涜鎺掑簭錛岃?/SPAN>order-by鍒欐槸鍦ㄦ暟鎹簱涓嬌鐢?/SPAN>SQL璇彞榪涜鎺掑簭銆?/SPAN>

 

<set name="images"

lazy="true"

table="ITEM_IMAGE"

order-by="IMAGE_NAME asc">

<key column="ITEM_ID"/>

<composite-element class="Image">

<property name="name" column="IMAGE_NAME" not-null="true"/>

<property name="filename" column="FILENAME" not-null="true"/>

<property name="sizeX" column="SIZEX" not-null="true"/>

<property name="sizeY" column="SIZEY" not-null="true"/>

</composite-element>

</set>

榪欓噷瀹氫箟浜嗕竴涓柊鐨?/SPAN>Image綾伙紝瀹冧笉鏄疄浣撶被(entity class)錛岃屾槸涓涓畝鍗曠殑鍊肩被鍨?/SPAN>(value type)錛屼粎浠呮槸鐖跺疄浣撶殑涓涓?/SPAN>component錛屽叾鐢熷懡鍛ㄦ湡瀹屽叏鐢卞寘鍚叾鐨勭埗瀹炰綋 (parent entity) ITEM鎺у埗銆傝繖閲?/SPAN>Image綾諱腑鍖呭惈鏈夋暟鎹?/SPAN>ITEM_ID銆?/SPAN> IMAGE_NAME銆?/SPAN>FILENAME銆?/SPAN>SIZEX銆佸拰SIZEY銆?/SPAN>

 

 

<set name="images"

lazy="true"

table="ITEM_IMAGE"

order-by="IMAGE_NAME asc">

<key column="ITEM_ID"/>

<composite-element class="Image">

<parent name="item"/>

<property name="name" column="IMAGE_NAME" not-null="true"/>

<property name="filename" column="FILENAME" not-null="true"/>

<property name="sizeX" column="SIZEX" not-null="true"/>

<property name="sizeY" column="SIZEY" not-null="true"/>

</composite-element>

</set>

鍓嶉潰瀹氫箟鐨?/SPAN>IMAGE綾諱腑錛屽茍涓嶅寘鍚湁瀵圭埗瀹炰綋ITEM鐨勫紩鐢ㄣ備篃灝辨槸璇達紝鍙彲浠ヤ粠ITEM涓鑸埌IMAGE涓紝浣嗘槸鍦?/SPAN>IMAGE鏃犳硶寰楀埌ITEM銆傝繖鍙互閫氳繃<parent>瀹炵幇銆傝繖鏍?/SPAN>IMAGE綾諱腑灝卞惈鏈変竴涓暟鎹垚鍛?/SPAN>item錛屾寚鍚戝叾鐖跺疄浣撱?/SPAN>

浣嗘槸錛屽畠鏄棤娉曞仛鍒扮湡姝g殑鍙屽悜瀵艱埅鐨勩備緥濡傦細閫氳繃SQL鏌ヨ鐩存帴寰楀埌IMAGE鍚庯紝浣嗘槸濡傚悓鏅氱殑鍊肩被鍨嬩竴鏍鳳紝IMAGE瀵硅薄鐨?/SPAN>item鏄?/SPAN>null錛屼綘鏃犳硶浠?/SPAN>component濡傛鐩存帴寰楀埌鍏剁埗瀵硅薄錛岃繖蹇呴』鐢遍氳繃鐖訛紞瀛愬叧緋葷殑entity瀹屾垚銆?/SPAN>



jinfeng_wang 2005-04-11 14:12 鍙戣〃璇勮
]]>
Hibernate Transaction Theory released.http://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3115.htmljinfeng_wangjinfeng_wangMon, 11 Apr 2005 03:59:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3115.htmlhttp://www.tkk7.com/jinfeng_wang/comments/3115.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/11/3115.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/3115.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/3115.htmlHibernate Transaction Theory released.

緲昏瘧鑺傞変簬銆奌ibernate in Action銆婥hapter 5錛屾湰鏂囨湭緇忓師浣滆呭悓鎰忥紝鍏惰憲浣滄潈涓哄師浣滆呮墍鏈夛紝璇峰嬁浠繪剰杞澆銆傚鏈夌炕璇戦棶棰橈紝璇蜂笌jinfeng_wang鑱旂郴銆?BR>
涓嬭澆鍦板潃錛?http://www.tkk7.com/Files/jinfeng_wang/Hibernate%20Transactions%20Theory.zip



jinfeng_wang 2005-04-11 11:59 鍙戣〃璇勮
]]>
hibernate transaction theory(2)http://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2945.htmljinfeng_wangjinfeng_wangThu, 07 Apr 2005 08:10:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2945.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2945.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2945.html#Feedback1http://www.tkk7.com/jinfeng_wang/comments/commentRss/2945.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2945.html5.2 Working with application transactions

In our CaveatEmptor application, both the user who posted a comment and any system administrator can open an Edit Comment screen to delete or edit the text of a comment. Suppose two different administrators open the edit screen to view the same comment simultaneously. Both edit the comment text and submit their changes. At this point, we have three ways to handle the concurrent attempts to

write to the database:

鈻?Last commit wins鈥?/SPAN>Both updates succeed, and the second update overwrites the changes of the first. No error message is shown.

鈻?First commit wins鈥?/SPAN>The first modification is persisted, and the user submitting the second change receives an error message. The user must restart the business process by retrieving the updated comment. This option is often called optimistic locking.

鈻?SPAN style="FONT: 7pt 'Times New Roman'">        Merge conflicting updates鈥?/SPAN>The first modification is persisted, and the second modification may be applied selectively by the user.

鍦ㄦ垜浠殑CavertEmptor涓紝鐢ㄦ埛鍜岀郴緇熺鐞嗗憳閮藉彲浠ユ墦寮鈥滅紪杈?/SPAN>cooment鈥濆姛鑳斤紝鍒犻櫎鎴栬呯紪杈戣瘎璁恒傚亣璁懼悓鏃舵湁涓や釜綆$悊鍛樺鍚屼竴涓瘎璁烘墦寮緙栬緫鍔熻兘錛屼粬浠兘瀵硅瘎璁鴻繘琛岀紪杈戠劧鍚庢彁浜ゃ傝繖鏄紝鎴戜滑鍙戠幇鍙兘鏈変笁縐嶆柟寮忓姝ゅ茍鍙戞搷浣滆繘琛屽鐞嗐佸啓鍏ユ暟鎹簱錛?/SPAN>

   鍚庤呮彁浜よ幏鑳滐紞涓ゆ鏇存柊閮芥垚鍔燂紝浣嗘槸絎簩嬈℃洿鏂板皢浼氳鐩栫涓嬈$殑鏇存柊錛屼笉鏄劇ず浠諱綍閿欒銆?/SPAN>

   鍓嶈呮彁浜よ幏鑳滐紞絎竴涓彁浜よ呮垚鍔燂紝鏇存柊鏁版嵁搴擄紝鍚庝竴涓彁浜ゅけ璐ワ紝騫惰繑鍥為敊璇俊鎭傜敤鎴峰繀欏婚噸鏂板紑濮嬫暣涓笟鍔¤繃紼嬶紝鍙栧緱琚洿鏂扮殑璇勮銆傝呭氨鏄父璇寸殑鈥滀箰瑙傞攣鈥濄?/SPAN>

   鍚堝茍鏇存柊鍐茬獊錛嶇涓涓洿鏂拌淇濆瓨錛岃岀浜屼釜鏇存柊鍒欑敱鐢ㄦ埛閫夋嫨鏄惁淇濆瓨銆?/SPAN>

 

The first option, last commit wins, is problematic; the second user overwrites the changes of the first user without seeing the changes made by the first user or even knowing that they existed. In our example, this probably wouldn鈥檛 matter, but it would be unacceptable for some other kinds of data. The second and third options are usually acceptable for most kinds of data. From our point of view, the third option is just a variation of the second鈥攊nstead of showing an error message, we show the message and then allow the user to manually merge changes. There is no single best solution. You must investigate your own business requirements to decide among these three options.

涓婇潰鐨勭涓涓柟妗堟槸鏈夐棶棰樼殑錛氱浜屼釜鐢ㄦ埛鍦ㄤ笉鐭ラ亾鏀瑰彉銆佺敋鑷充笉鐭ラ亾絎竴涓敤鎴峰凡緇忔搷浣滅殑鎯呭喌涓嬶紝鐩存帴閲嶅啓浜嗘暟鎹簱銆傚湪鎴戜滑鐨勪緥瀛愪腑錛屼篃璁告槸娌″叧緋葷殑錛屼絾鏄浜庢煇浜涘簲鐢ㄦ槸涓嶅彲鎺ュ彈鐨勩傜浜屼釜鍜岀涓変釜鏂規瀵逛簬緇濆ぇ閮ㄥ垎鏁版嵁鏄彲浠ョ粨鏉熺殑銆傛寜鐓ф垜浠殑瑙傜偣錛岀涓変釜鏄浜屼釜鐨勫彉縐嶏紝瀹冧笉鏄樉寮忛敊璇俊鎭紝鑰屾槸緇欑敤鎴瘋繘琛岄夋嫨錛屽厑璁哥敤鎴鋒墜宸ュ悎騫跺鏁版嵁鐨勪慨鏀廣傝繖閲屽茍娌℃湁涓涓渶瀹岀編鐨勮В鍐蟲柟妗堛備綘蹇呴』鏍規嵁浣犺嚜宸遍」鐩殑闇瑕佽嚜琛岄夋嫨鏂規銆?/SPAN>

 

The first option happens by default if you don鈥檛 do anything special in your application銆?/SPAN>On the other hand, Hibernate can help you implement the second and third strategies, using managed versioning for optimistic locking.

濡傛灉鍦ㄤ綘鐨勯」鐩腑涓嶉噰鍙栦換浣曟帾鏂斤紝閭d箞浼氶粯璁や嬌鐢ㄧ涓涓柟妗堛備絾鏄紝hibernate鍙互閫氳繃浣跨敤瀵光滀箰瑙傞攣鈥濅嬌鐢ㄢ滅鐞嗙増鏈濓紝甯姪浣犲疄鐜扮浜屼釜鎴栫涓変釜鏂規銆?/SPAN>

 

Managed versioning relies on either a version number that is incremented or a timestamp that is updated to the current time, every time an object is modified. For Hibernate managed versioning, we must add a new property to our Comment class and map it as a version number using the <version> tag. First, let鈥檚 look at the changes to the Comment class:

public class Comment {

...

private int version;

...

void setVersion(int version) {

this.version = version;

}

int getVersion() {

return version;

}

}

鈥滅鐞嗙増鏈濅嬌鐢ㄧ増鏈彿鎴栬呮椂闂存埑瀵規暟鎹繘琛屾爣璁幫紝姣忔鏁版嵁鍦ㄦ洿鏂扮殑鏃跺欙紝瀹冮兘浼氳鏇存柊銆傚浜?/SPAN>hibernate錛屾垜浠繀欏葷粰Comment綾伙紝娣誨姞鏂扮殑灞炴э紝鐒跺悗浣跨敤<version>鏍囩瀵瑰叾榪涜鏄犲皠銆傞鍏堣鎴戜滑鏉ョ湅涓涓?/SPAN>Comment綾葷殑鏀瑰彉錛?/SPAN>

public class Comment {

...

private int version;

...

void setVersion(int version) {

this.version = version;

}

int getVersion() {

return version;

}

}

 

You can also use a public scope for the setter and getter methods. The <version> property mapping must come immediately after the identifier property mapping in the mapping file for the Comment class:

<class name="Comment" table="COMMENTS">

<id ...

<version name="version" column="VERSION"/>

...

</>

浣犲彲浠ヤ嬌鐢?/SPAN>public鐨?/SPAN>setter鎴?/SPAN>getter鏂規硶錛岃繖閲岀殑<version>鏄犲皠鐨勫畾涔夊繀欏葷揣璺熷湪鏄犲皠鏂囦歡涓?/SPAN>comment綾葷殑id鍚庨潰銆?/SPAN>

<class name="Comment" table="COMMENTS">

<id ...

<version name="version" column="VERSION"/>

...

</>

 

You don鈥檛 need to set the value of the version or timestamp property yourself; Hibernate will initialize the value when you first save a Comment, and increment or reset it whenever the object is modified. Whenever Hibernate updates a comment, it uses the version column in the SQLWHERE clause:

update COMMENTS set COMMENT_TEXT='New comment text', VERSION=3

where COMMENT_ID=123 and VERSION=2

浣犳棤闇鑷繁鎵嬪伐鍘諱慨鏀硅繖閲岀殑鐗堟湰鍙鋒垨鑰呮椂闂存埑錛?/SPAN>hibernate灝嗕細鍦ㄤ綘淇濆瓨Commment鐨勬椂鍊欏鍏跺垵濮嬪寲錛岀劧鍚庡湪瀵硅薄姣忔琚洿鏂扮殑鏃跺欏嵆浣挎洿鏂板叾鐗堟湰鍙峰拰鏃墮棿鎴熾傛棤璁?/SPAN>hibernate浣曟椂鏇存柊comment,瀹冮兘浼氬湪SQL璇彞鐨?/SPAN>where鍚庨潰鍔犱笂version鍒楋細

update COMMENTS set COMMENT_TEXT='New comment text', VERSION=3

where COMMENT_ID=123 and VERSION=2

 

If another application transaction would have updated the same item since it was read by the current application transaction, the VERSION column would not contain the value 2, and the row would not be updated. Hibernate would check the row count returned by the JDBC driver鈥攚hich in this case would be the number of rows updated, zero鈥攁nd throw a StaleObjectStateException. Using this exception, we might show the user of the second application transaction an error message (鈥淵ou have been working with stale data because another user modified it!鈥? and let the first commit win. Alternatively, we could catch the exception and show the second user a new screen, allowing the user to manually merge changes between the two versions.

濡傛灉鏌愪釜浜嬪姟宸茬粡鏇存柊浜嗘煇涓暟鎹紝鑰屽彟澶栦竴涓簨鍔′篃鍑嗗鏇存柊姝ゆ暟鎹紝浣嗘槸褰撴椂瀹冨彇寰楁暟鎹殑鐗堟湰鍙鋒槸2錛岃屾鏃剁敱浜庤鏁版嵁宸茬粡琚洿鏂拌繃錛屾柊鐗堟湰鍙鋒槸3錛屽洜姝ゅ皢鏃犳硶鏇存柊鏁版嵁搴撲腑鐨勪換浣曠殑鏁版嵁銆傝繖鏃跺?/SPAN>hibernate灝嗕細媯鏌?/SPAN>jdbc鐨勮繑鍥炶鏁幫紞濡傛灉鏈湁浠諱綍鏁版嵁琛岃鏇存柊錛屽垯榪斿洖0錛嶅洜姝ゅ皢浼氭姏鍑?/SPAN>StaleObjectStateException寮傚父銆?/SPAN>

 

Using this exception, we might show the user of the second application transaction an error message (鈥淵ou have been working with stale data because another user modified it!鈥? and let the first commit win. Alternatively, we could catch the exception and show the second user a new screen, allowing the user to manually merge changes between the two versions.

浣跨敤姝ゅ紓甯革紝灝卞彲浠ョ粰絎簩涓簲鐢ㄤ簨鍔$殑鐢ㄦ埛鏄懼紡閿欒淇℃伅錛堜綘姝d嬌鐢ㄧ殑鏁版嵁宸茬粡琚叾浠栦漢鏇存柊浜嗭級錛岃絎竴涓?/SPAN>鎻愪氦鑰呰幏鑳?/SPAN>銆傚綋鐒訛紝浣犱篃鍙互鎹曟崏寮傚父錛岀劧鍚庣粰絎簩涓敤鎴蜂竴涓柊鐨勫鉤闈紝鍏佽瀹冩墜宸ュ悎騫惰繖涓や釜鐗堟湰鐨勬暟鎹?/SPAN>

]]>
hibernate Transactions theory (1)http://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2916.htmljinfeng_wangjinfeng_wangThu, 07 Apr 2005 06:03:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2916.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2916.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/07/2916.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/2916.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2916.html闃呰鍏ㄦ枃

jinfeng_wang 2005-04-07 14:03 鍙戣〃璇勮
]]>
hibernate's session鍜屻婁紒涓氬簲鐢ㄦ灦鏋勬ā寮忋嬬11绔?/title><link>http://www.tkk7.com/jinfeng_wang/archive/2005/04/05/2869.html</link><dc:creator>jinfeng_wang</dc:creator><author>jinfeng_wang</author><pubDate>Tue, 05 Apr 2005 03:13:00 GMT</pubDate><guid>http://www.tkk7.com/jinfeng_wang/archive/2005/04/05/2869.html</guid><wfw:comment>http://www.tkk7.com/jinfeng_wang/comments/2869.html</wfw:comment><comments>http://www.tkk7.com/jinfeng_wang/archive/2005/04/05/2869.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/jinfeng_wang/comments/commentRss/2869.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/jinfeng_wang/services/trackbacks/2869.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><B><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN">hibernate瀹炵幇浜嗐婁紒涓氬簲鐢ㄦ灦鏋勬ā寮忋嬬11绔犵殑璁稿妯″紡錛岀粨鍚堣搗鏉ュ垯鏇磋兘鐞嗚Вhibernate鐨剆ession鐨勪綔鐢ㄣ傚彧鏄弻鏂圭殑涓浜涙湳璇湭蹇呰兘鐩存帴瀵瑰簲涓娿?BR>錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛?BR>Session鎺ュ彛<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN">Session錛堜細璇濓級鎺ュ彛鏄疕ibernate搴旂敤浣跨敤鐨勪富瑕佹帴鍙c備細璇濇帴鍙g殑瀹炰緥鏄交閲忕駭鐨勫茍涓斿垱寤轟笌閿姣佺殑浠d環涔熶笉鏄傝吹銆傝繖寰堥噸瑕佸洜涓轟綘鐨勫簲鐢ㄥ彲鑳藉緇堝湪鍒涘緩涓庨攢姣佷細璇濓紝鍙兘姣忎竴嬈¤姹傞兘浼氬姝ゃ侶ibernate浼氳瘽騫朵笉鏄嚎紼嬪畨鍏ㄧ殑鍥犳搴旇琚璁′負姣忔鍙兘鍦ㄤ竴涓嚎紼嬩腑浣跨敤銆?o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"> <o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN">Hibernate浼氳瘽鏄竴涓粙浜庤繛鎺ュ拰浜嬪姟涔嬮棿鐨勬蹇點?FONT color=#ff1493>浣犲彲浠ョ畝鍗曞湴璁や負浼氳瘽鏄浜庝竴涓崟鐙殑宸ヤ綔鍗曞厓宸茶杞藉璞$殑緙撳瓨鎴栭泦鍚堛侶ibernate鍙互媯嫻嬪埌榪欎釜宸ヤ綔鍗曞厓涓璞$殑鏀瑰彉銆?/FONT>鎴戜滑鏈夋椂涔熷皢浼氳瘽縐頒負鎸佺畫鎬х鐞嗗櫒錛屽洜涓哄畠涔熸槸涓庢寔緇ф湁鍏崇殑鎿嶄綔渚嬪瀛樺偍鍜屽彇鍑哄璞$殑鎺ュ彛銆傛敞鎰忥紝Hibernate浼氳瘽涓嶹eb灞傜殑HttpSession娌℃湁浠諱綍鍏崇郴銆傚綋鎴戜滑鍦ㄦ湰涔︿腑浣跨敤浼氳瘽鏃訛紝鎴戜滑鎸囩殑鏄疕ibernate浼氳瘽銆備負浜嗗尯鍒紝鏈夋椂鎴戜滑灝咹ttpSession瀵硅薄縐頒負鐢ㄦ埛浼氳瘽銆?o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"> <o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><B><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">SessionFactory</SPAN></B><B><SPAN style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">鎺ュ彛</SPAN></B><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN">搴旂敤浠?/SPAN><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-bidi-font-size: 9.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">SessionFactory</SPAN><SPAN style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN; mso-bidi-font-size: 9.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">錛堜細璇濆伐鍘傦級閲岃幏寰椾細璇濆疄渚嬨備笌浼氳瘽鎺ュ彛鐩告瘮錛岃繖涓璞′笉澶熶護浜哄叴濂嬨?/SPAN><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"> <o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN">浼氳瘽宸ュ巶褰撶劧涓嶆槸杞婚噺綰х殑錛佸畠鎵撶畻鍦ㄥ涓簲鐢ㄧ嚎紼嬮棿榪涜鍏變韓銆傚吀鍨嬪湴錛?FONT color=#ff1493>鏁翠釜搴旂敤鍙湁鍞竴鐨勪竴涓細璇濆伐鍘傗斺斾緥濡傚湪搴旂敤鍒濆鍖栨椂琚垱寤恒傜劧鑰岋紝濡傛灉浣犵殑搴旂敤浣跨敤<SPAN lang=EN-US>Hibernate璁塊棶澶氫釜鏁版嵁搴擄紝浣犻渶瑕佸姣忎竴涓暟鎹簱浣跨敤涓涓細璇濆伐鍘傘?o:p></o:p></SPAN></FONT></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN"> <o:p></o:p></SPAN></P><SPAN style="FONT-SIZE: 10.5pt; FONT-FAMILY: 瀹嬩綋; mso-fareast-language: ZH-CN; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-bidi-language: AR-SA">浼氳瘽宸ュ巶緙撳瓨浜嗙敓鎴愮殑<SPAN lang=EN-US>SQL璇彞鍜孒ibernate鍦ㄨ繍琛屾椂浣跨敤鐨勬槧灝勫厓鏁版嵁銆傚畠涔熶繚瀛樹簡鍦ㄤ竴涓伐浣滃崟鍏冧腑璇誨叆鐨勬暟鎹茍涓斿彲鑳藉湪浠ュ悗鐨勫伐浣滃崟鍏冧腑琚噸鐢紙鍙湁綾誨拰闆嗗悎鏄犲皠鎸囧畾浜嗚繖縐嶄簩綰х紦瀛樻槸鎯寵鐨勬椂鎵嶄細濡傛錛夈?BR><BR><BR></SPAN></SPAN><img src ="http://www.tkk7.com/jinfeng_wang/aggbug/2869.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/jinfeng_wang/" target="_blank">jinfeng_wang</a> 2005-04-05 11:13 <a href="http://www.tkk7.com/jinfeng_wang/archive/2005/04/05/2869.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>hibernate's casade table.http://www.tkk7.com/jinfeng_wang/archive/2005/04/03/2798.htmljinfeng_wangjinfeng_wangSun, 03 Apr 2005 13:43:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/03/2798.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2798.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/03/2798.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/2798.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2798.html 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>

<hibernate-mapping>
  
<class name="com.oreilly.hh.Album" table="ALBUM">
    
<meta attribute="class-description">
      Represents an album in the music database, an organized list of tracks.
      @author Jim Elliott (with help from Hibernate)
    
</meta>

    
<id name="id" type="int" column="ALBUM_ID">
      
<meta attribute="scope-set">protected</meta>
      
<generator class="native"/>
    
</id>

    
<property name="title" type="string">
      
<meta attribute="use-in-tostring">true</meta>
      
<column name="TITLE" not-null="true" index="ALBUM_TITLE"/>
    
</property>

    
<property name="numDiscs" type="integer" not-null="true"/>

    
<set name="artists" table="ALBUM_ARTISTS">
      
<key column="ALBUM_ID"/>
      
<many-to-many class="com.oreilly.hh.Artist" column="ARTIST_ID"/>
    
</set>

    
<set name="comments" table="ALBUM_COMMENTS">
      
<key column="ALBUM_ID"/>
      
<element column="COMMENT" type="string"/>
    
</set>

    
<list name="tracks" table="ALBUM_TRACKS" cascade="all">
      
<meta attribute="use-in-tostring">true</meta>
      
<key column="ALBUM_ID"/>
      
<index column="POS"/>
      
<composite-element class="com.oreilly.hh.AlbumTrack">
        
<many-to-one name="track" class="com.oreilly.hh.Track" cascade="all">
          
<meta attribute="use-in-tostring">true</meta>
          
<column name="TRACK_ID"/>
        
</many-to-one>
        
<property name="disc" type="integer" not-null="true"/>
        
<property name="positionOnDisc" type="integer" not-null="true"/>
      
</composite-element>
    
</list>

    
<property name="added" type="date">
      
<meta attribute="field-description">When the album was created</meta>
    
</property>

  
</class>
</hibernate-mapping>


 

The cascade attribute tells Hibernate that you want operations performed on a 'parent' object to be transitively applied to its 'child' or 'dependent' objects. It's applicable to all forms of collections and associations. There are several possible values to choose among. The most common are none (the default), save-update, delete, and all (which combines save-update and delete). You can also change the default from none to save-update throughout your entire mapping document by supplying a default-cascade attribute in the hibernate-mapping tag itself.

綰ц仈錛坈ascade錛夊睘鎬у憡璇塰ibernate錛氭墍鏈夊鐖跺璞$殑鎿嶄綔閮藉皢浠樿浜庡瓙瀵硅薄鍜屼緷璧栧璞′笂銆傜駭鑱斿彲浠ュ簲鐢ㄤ簬鍚勭褰㈠紡鐨勯泦鍚堝拰鍏寵仈涓傝繖閲岀駭鑱旂殑鍏蜂綋鍙栧煎彲浠ユ湁澶氫釜錛屾渶甯哥敤鐨勬湁錛歯one(榛樿鍊?錛宻ave-update,delete鍜宎ll錛堝叾涓寘鍚簡save-update鍜宒elete錛夈備綘鍙互鍦╤ibernate-mappiong tag涓氳繃璁劇疆default-casade灞炴э紝淇敼榪欓噷鐨刢asade鐨勯粯璁ゅ鹼紝渚嬪淇敼鍏墮粯璁ゅ間負save-update銆?BR>
In our example, we want the tracks owned by an album to be automatically managed by the album, so that when we delete the album, its tracks are deleted. Note that we need to apply the
cascade attribute both to the tracks collection and its constituent track element to achieve this. Also, by using a cascade value of all, we eliminate the need to explicitly save any Track objects we create for the album鈥攖he addAlbumTrack() method of Example 5-7 no longer needs the line:

   session.save(track);

鍦ㄦ垜浠殑渚嬪瓙涓紝鎴戜滑甯屾湜track瀹屽叏鐢盿lbum鑷姩鎺岀錛屽綋鎴戜滑鍒犻櫎album鏃訛紝瀹冪殑track涔熶細琚垹闄ゃ傛敞鎰忥紝榪欓噷鎴戜滑闇瑕佸湪涓ゅ璁劇疆casade灞炴у疄鐜版浠誨姟錛歵racks闆嗗悎鍜屽畠鐨勭粍鎴愬厓绱爐rack銆傚悓鏍鳳紝鐢變簬鎴戜滑璁瞔asade璁劇疆涓篴ll錛岄偅涔堜笉鍐嶉渶瑕佹樉寮忕殑淇濆瓨涓篴lbum鍒涘緩鐨則rack瀵硅薄錛?-7渚嬪瓙涓殑addAlbumTrack鏂規硶涓嶅啀闇瑕佽皟鐢╯ession.save(track)鏂規硶銆?BR>

By telling Hibernate that it's fully responsible for the relationship between an album and its track, we enable it to persist tracks when they're added to the album as well as delete them when the album itself is deleted.


閫氳繃楂樼煡hibernate錛岀敱鍏跺畬鍏ㄨ礋璐lbum鍜宼rack鐩存帴鐨勫叧緋伙紝鍙互淇濊瘉track鍦ㄥ姞鍏ュ埌album鏃惰嚜鍔ㄤ繚瀛橈紝鍦╝lbum鍒犻櫎鏃惰嚜鍔ㄥ垹闄ゃ?BR>
Delegating this sort of bookkeeping to the mapping layer can be very convenient, freeing you to focus on more abstract and important tasks, so it is worth using when appropriate. It's reminiscent of the liberation provided by Java's pervasive garbage collection, but it can't be as comprehensive because there is no definitive way to know when you're finished with persistent data by performing reachability analysis; you need to indicate it by calling
delete() and establishing lifecycle connections. The trade-off between flexibility and simple automation is yours to make, based on the nature of your data and the needs of your project.
灝嗚繖鏍風殑浠誨姟瀹屽叏浜や唬緇欐寔涔呭眰浣垮緱鎴戜滑鐨勪換鍔℃洿鍔犵殑綆渚匡紝鍙互璁╂垜浠皢娉ㄦ剰鍔涙姇娉ㄤ簬鏇存娊璞℃洿閲嶈鐨勪笟鍔★紝鍥犳鎭板綋鐨勪嬌鐢╟asade鏄煎緱鐨勶紝榪欏氨鍍廽ava鐨勫瀮鍦炬敹闆嗕竴鏍鳳紝浣挎垜浠殑宸ヤ綔寰楀埌浜嗕竴瀹氱殑瑙f斁銆備絾鏄紝鐢變簬鐩墠騫舵病鏈変換浣曠殑鍙揪鎬у垎鏋愭柟娉曪紝鍙互紜垏鐨勭煡閬撯滀綘鏄惁宸茬粡涓嶅啀闇瑕佹鎸佷箙灞傛暟鎹濓紝鍥犳浣犲繀欏昏皟鐢╠elete()鏂規硶錛屾墠鑳藉緩绔嬭搗姝ょ敓鍛藉懆鏈熼摼鎺ャ備綘蹇呴』渚濇嵁浣犵殑鏁版嵁鐨勭壒鐐瑰拰欏圭洰鐨勯渶瑕侊紝鍦ㄧ伒媧繪у拰鑷姩鍖栫殑綆鍗曟ц繘琛屾潈琛°?BR>
For example, if you use Collections methods to remove a Track from an Album's tracks property, this breaks the link between the Album and Track but does not actually delete the Track record. Even if you later delete the entire Album, this Track will remain, because it wasn't linked to the Album at the time that it was deleted.
渚嬪錛氬鏋滀綘浣跨敤Album鐨則racks collection鐨剅emove鏂規硶錛屽垹闄や竴涓猼rack錛岃繖灝嗕細縐婚櫎album鍜宼rack鐩存帴鐨勯摼鎺ワ紝浣嗘槸瀹冨茍娌$湡姝g殑鍒犻櫎track璁板綍銆傚嵆浣夸綘鍚庢潵鏁翠釜鐨勫垹闄lbum錛岃繖涓猼rack涔熷皢浼氱戶緇繚鐣欑潃錛屽洜涓烘鏃惰track宸茬粡鍜宎lbum澶卞幓浜嗛摼鎺ャ?BR>//錛?璇戣呮敞錛夎繖閲岀殑鍚箟鏄細綰ц仈鏄湁鑼冨洿鐨勶紝琛ˋLBUM鍜岃〃TRACK涔嬮棿閫氳繃琛ˋlbum_Tracks寤虹珛浜嗚仈緋匯傚湪albu.hbm.xml涓紝榪涜浜嗙駭鑱旈厤緗紝浣垮緱Album鍜孉LBUM_TRACKS涔嬮棿綰ц仈銆傚綋鍒犻櫎鏌愪釜album鏃訛紝閭d箞鐩稿簲鐨凙LBUM_TRACKS涓殑璁板綍涔熷皢鍒犻櫎銆傚鏋滃album涓殑tracks鎵цremove鏂規硶鏃訛紝閭d箞ALBUM_TRACKS涓殑鐩稿簲璁板綍涔熷皢鍒犻櫎銆備絾鏄繖閲岀殑綰ц仈鍗存棤娉曞歡浼稿埌琛═RACK涓紝TRACK琛ㄤ腑鐨勮褰曞皢瀹夌劧鏃犳仚錛屼緷鐒跺瓨鍦ㄣ?BR>//鍘熸枃涓殑tracks鍑虹幇鍦ㄤ袱澶勶紝涓涓槸Album瀵硅薄鐨勫睘鎬э紝鍏跺搴旂潃ALBUM_TRACKS鐨勮褰曘備竴涓垯鏄崟鐙殑TRACK琛紝鎵浠ヨ〃榪板崄鍒嗗鏄撳紩璧鋒販娣嗐?/FONT>


]]>
hibernate's transient object and persistent objecthttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2706.htmljinfeng_wangjinfeng_wangFri, 01 Apr 2005 06:21:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2706.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2706.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2706.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/2706.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2706.html

package com.oreilly.hh;

import net.sf.hibernate.
*;
import net.sf.hibernate.cfg.Configuration;

import java.sql.Time;
import java.util.Date;

/**
 * Create sample data, letting Hibernate persist it for us.
 
*/

public class CreateTest {

    
public static void main(String args[]) throws Exception {
        
// Create a configuration based on the properties file we've put
        
// in the standard place.
        Configuration config = new Configuration();

        
// Tell it about the classes we want mapped, taking advantage of
        
// the way we've named their mapping documents.
        config.addClass(Track.class);

        
// Get the session factory we can use for persistence
        SessionFactory sessionFactory = config.buildSessionFactory();

        
// Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx 
= null;
        
try {
            
// Create some data and persist it
            tx = session.beginTransaction();

            Track track 
= new Track("Russian Trance",
                                    
"vol2/album610/track02.mp3",
                                    Time.valueOf(
"00:03:30"), new Date(),
                                    (
short)0);
            session.save(track);

            track 
= new Track("Video Killed the Radio Star",
                              
"vol2/album611/track12.mp3",
                              Time.valueOf(
"00:03:49"), new Date(),
                              (
short)0);
            session.save(track); 
            
             track 
= new Track("Gravity's Angel",
                              
"vol2/album175/track03.mp3",
                               Time.valueOf(
"00:06:06"), new Date(),
                               (
short)0);
            session.save(track);

            
// We're done; make our changes permanent
            tx.commit();

        }
 catch (Exception e) {
            
if (tx != null{
                
// Something went wrong; discard all partial changes
                tx.rollback();
            }

            
throw e;
        }
 finally {
            
// No matter what, close the session
            session.close();
        }


        
// Clean up after ourselves
        sessionFactory.close();
    }

}



When it comes time to actually perform persistence, we ask the SessionFactory to open a Session for us (line 27), which establishes a JDBC connection to the database, and provides us with a context in which we can create, obtain, manipulate, and delete persistent objects. As long as the session is open, a connection to the database is maintained, and changes to the persistent objects associated with the session are tracked so they can be applied to the database when the session is closed. Conceptually you can think of a session as a 'large scale transaction' between the persistent objects and the database, which may encompass several database-level transactions. Like a database transaction, though, you should not think about keeping your Hibernate session open over long periods of application existence (such as while you're waiting for user input). A single session is used for a specific and bounded operation in the application, something like populating the user interface or making a change that has been committed by the user. The next operation will use a new session. Also note that Session objects are not thread safe, so they cannot be shared between threads. Each thread needs to obtain its own session from the factory.

鐜板湪寮濮嬭榪扳滄寔涔呭寲鎿嶄綔鈥濄傚湪絎?7琛岋紝浣跨敤SessionFactory鎵撳紑浜哠ession錛屽緩绔嬩簡鏁版嵁搴揓DBC鐨勮繛鎺ワ紝錛堣榪炴帴宸茬粡榪涜浜嗗垵濮嬪寲錛夛紝鎴戜滑鍙互鍊熺敤瀹冭繘琛屼竴緋誨垪鐨勬搷浣滐紝渚嬪錛氬垱寤恒佽幏鍙栥佹搷綰點佸垹闄ゆ寔涔呭寲瀵硅薄銆傚彧瑕乻ession澶勪簬open鐘舵侊紝閭d箞瀵規暟鎹簱鐨勮繛鎺ュ氨涓鐩村緩绔嬬潃錛屾墍鏈夊宸茬粡鍜宻ession緇戝畾鐨勬寔涔呭寲瀵硅薄鐨勬搷浣滈兘浼氳璁板綍涓嬫潵錛屽茍涓斿湪session鍏抽棴鐨勬椂鍊欙紝鎵鏈夌殑榪欎簺鎿嶄綔灝變細琚洿鏂板埌鏁版嵁搴撲腑銆備粠姒傚康涓婄悊瑙o紝浣犲彲浠ュ皢session鐪嬩綔涓涓湪鎸佷箙鍖栧璞″拰鏁版嵁搴撲箣闂寸殑鈥滃ぇ瑙勬ā鐨勪簨鍔♀濓紝瀹冨彲浠ヨ法瓚婂嚑涓暟鎹簱浜嬪姟銆傛濡傛暟鎹簱浜嬪姟閭f牱錛屼綘涓嶈鍦ㄧ▼搴忎腑灝唄ibernate session闀挎椂闂寸殑鎵撳紑鐫錛堜緥濡傚綋紼嬪簭鍦ㄧ瓑寰呯敤鎴瘋緭鍏ョ殑鏃跺欙級錛岀▼搴忎腑鐨勬瘡涓崟鐙殑鎿嶄綔閮借閲囩敤鍗曠嫭鐨剆ession錛屼緥濡傚湪鈥滃脊鍑虹敤鎴風晫闈紝鎴栬呭皢鐢ㄦ埛鐨勪慨鏀規彁浜ゅ埌鏁版嵁搴撯濅腑錛屽氨璇ヤ嬌鐢ㄤ袱涓猻ession銆傚悓鏍烽渶瑕佹敞鎰忕殑鏄紝session鏄嚎紼嬩笉瀹夊叏鐨勶紝鍥犳鏃犳硶鍦ㄧ嚎紼嬩箣闂村叡浜玸ession錛屾瘡涓嚎紼嬮兘搴旇浣跨敤SessionFactory寤虹珛鑷繁鐨剆ess銆?BR>
We need to look more closely at the lifecycle of mapped objects in Hibernate, and how this relates to sessions, because the terminology is rather specific and the concepts are quite important. A mapped object such as an instance of our Track class moves back and forth between two states with respect to Hibernate: transient and persistent. An object that is transient is not associated with any session. When you first create a Track instance using new(), it is transient; unless you tell Hibernate to persist it, the object will vanish when it is garbage collected or your application terminates.

涓嬮潰鎴戜滑瀵筯ibernate涓鏄犲皠瀵硅薄鐨勫0鏄庡懆鏈熻繘琛岃鏄庯紝鍥犱負琚槧灝勫璞$浉鍏崇殑鏈鏄浉褰撶壒鍒殑銆佸畠鐨勬蹇典篃鏄緢閲嶈鐨勩傚湪hibernate涓紝琚槧灝勫璞★紙渚嬪紼嬪簭涓殑Track瀵硅薄錛変細鍦ㄤ袱涓姸鎬佷腑涓嶆柇鐨勬潵鍥炲垏鎹細涓存椂錛坱ransient錛夌姸鎬佸拰鎸佷箙錛坧ersistent錛夌姸鎬併傛湭鏇句笌session緇戝畾鐨勫璞″氨澶勪簬涓存椂鐘舵侊紝渚嬪紼嬪簭涓垰寮濮媙ew鍑烘潵鐨凾rack瀵硅薄錛屽畠灝卞浜庝復鏃剁姸鎬併傞櫎闈炰綘閫氱煡hibernate瀵瑰畠榪涜鎸佷箙鍖栵紝鍚﹀垯鏁翠釜瀵硅薄灝變細鍦ㄥ瀮鍦炬敹闆嗗櫒鍥炴敹鎴栬呯▼搴忕粨鏉熺殑鏃跺欐秷閫濄?BR>
Passing a transient mapped object to a Session's save() method causes it to become persistent. It will survive garbage collection and termination of the Java VM, staying available until it is explicitly deleted. (There is a related distinction between entities and values discussed at the beginning of Appendix A. Mapped objects that have been persisted are called entities, even if they do not currently exist as an instance in any virtual machine.) If you've got a persistent object and you call Session's delete() method on it, the object transitions back to a transient state. The object still exists as an instance in your application, but it is no longer going to stay around unless you change your mind and save it again; it's ceased being an entity.

浣嗘槸錛屽皢涓涓復鏃跺璞′嬌鐢╯ession鐨剆ave鏂規硶淇濆瓨涔嬪悗錛屽畠灝卞浜庢寔涔呯姸鎬侊紝鍗充嬌鍦ㄥ瀮鍦懼洖浜嬫垨鑰匤ava VM緇撴潫涔嬪悗錛屽畠閮戒竴鐩村瓨鍦紝鐭ラ亾璇ュ璞¤鏄庣‘鍒犻櫎涓烘錛堝湪闄勫綍A寮澶存湁鐩稿叧鈥渆ntities鍜寁alues鍖哄埆鈥濈殑璁ㄨ錛屽嵆浣垮叾鍦ㄨ櫄鎷熸満涓茍涓嶇湡瀹炲瓨鍦ㄧ潃涓涓璞′笌涔嬬浉瀵瑰簲錛夈傚綋浣犲鎸佷箙鍖栧璞¤皟鐢╯ession.delete()鏂規硶鏃訛紝璇ュ璞″張鍙樹負涓存椂鐘舵併傝櫧鐒惰瀵硅薄浠嶇劧鍦ㄧ▼搴忎腑鍋氫負涓涓疄渚嬪瓨鍦ㄧ潃錛屼絾鏄櫎闈炰綘鏀瑰彉涓繪剰鍐嶆灝嗗叾鎸佷箙鍖栵紝閭d箞瀹冨皢寰堝揩鐨勬秷閫濓紝瀹冪殑鈥滃疄浣?entity)鈥濅篃灝辨剷鐒惰屼箣銆?/P>

On the other hand, and this point is worth extra emphasis, if you haven't deleted an object (so it's still persistent), when you change its properties there is no need to save it again for those changes to be reflected in the database. Hibernate automatically tracks changes to any persistent objects and flushes those changes to the database at appropriate times. When you close the session, any pending changes are flushed.

鍙︿竴鏂歸潰錛岄渶瑕佸己璋冪殑鏄細濡傝嫢浣犱笉鍒犻櫎鏌愭寔涔呭寲瀵硅薄錛岄偅涔堝綋浣犳敼鍙樺叾灞炴ф椂錛屽茍涓嶉渶瑕佹樉紺虹殑瀵瑰叾鐨勬敼鍙樿繘琛屼繚瀛樸俬ibernate灝嗕細鑷姩鐨勮窡韙埌浣犲鎸佷箙鍖栧璞$殑鏀瑰彉錛岀劧鍚庡湪閫傚綋鐨勬椂鍊欏皢榪欎簺鏀瑰彉濉叆鍒版暟鎹簱涓?BR>
An important but subtle point concerns the status of persistent objects you worked with in a session that has been closed, such as after you run a query to find all entities matching some criteria (you'll see how to do this in the upcoming section, 'Finding Persistent Objects'). As noted above, you don't want to keep this session around longer than necessary to perform the database operation, so you close it once your queries are finished. What's the deal with the mapped objects you've loaded at this point? Well, they were persistent while the session was around, but once they are no longer associated with an active session (in this case because the session has been closed) they are not persistent any longer. Now, this doesn't mean that they no longer exist in the database; indeed, if you run the query again (assuming nobody has changed the data in the meantime), you'll get back the same set of objects; they're still entities. It simply means that there is not currently an active correspondence being maintained between the state of the objects in your virtual machine and the database. It is perfectly reasonable to carry on working with the objects. If you later need to make changes to the objects and you want the changes to 'stick,' you will open a new session and use it to save the changed objects. Because each entity has a unique ID, Hibernate has no problem figuring out how to link the transient objects back to the appropriate persistent state in the new session.

涓嬮潰璁ㄨ鈥滃湪session鍏抽棴鍚庯紝鍜宻ession緇戝畾鐨勬寔涔呭寲瀵硅薄鐨勭姸鎬佲濈殑闂銆備緥濡傚綋浣犳墽琛屼簡涓涓煡璇紝寰楀埌浜嗕綘鎵闇瑕佺殑瀹炰綋瀵硅薄涔嬪悗錛屾濡傚墠闈㈡墍榪扮殑閭f牱錛屼綘涓嶅簲璇ュ皢session淇濇寔榪囦箙錛屼嬌涔嬭秴鍑烘暟鎹簱鎿嶄綔鐨勮寖鍥達紝榪欐椂鍊欎綘灝嗕細鍏抽棴session銆傞偅涔堟鏃墮偅浜涘凡緇忚澆鍏ョ殑鏄犲皠瀵硅薄澶勪簬浠涔堢姸鎬佸憿錛熺瓟妗堟槸榪欐牱鐨勶紝鍦╯ession鐨勬墦寮鐨勮繃紼嬩腑錛屽畠鏄浜庢寔涔呭寲鐘舵侊紝浣嗘槸瀹冧竴鏃︿笉鍦ㄥ拰active session錛堝洜涓烘鏃秙ession宸茬粡鍏抽棴錛夌粦瀹氾紝閭d箞瀹冨氨澶勪簬鎸佷箙鍖栫姸鎬併備絾鏄紝榪欏茍涓嶆剰鍛崇潃浠栦滑鍦ㄦ暟鎹簱涓氨涓嶅啀瀛樺湪錛屼簨瀹炰笂錛屽鏋滀綘鍐嶆鎵ц鏌ヨ錛堝亣璁炬闂存棤浜轟慨鏀規暟鎹級錛岄偅涔堜綘灝嗗彲浠ュ緱鍒板悓鏍風殑闆嗗悎鏁版嵁銆備篃灝辨槸璇達紝铏氭嫙鏈轟腑鐨勫璞℃墍澶勭殑鐘舵佸拰鏁版嵁搴撲腑鐨勫疄浣撴暟鎹殑鐘舵佷箣闂村茍娌℃湁蹇呯劧鐨勮仈緋匯傝繖鏃跺欙紝浣犲彲浠ュ閭d簺瀵硅薄榪涜鑷繁鐨勬搷浣滐紝濡傛灉浣犳敼鍙樹簡涓浜涘璞$殑鏁版嵁錛屽茍鎯沖皢鍏跺瓨鍌ㄥ埌鏁版嵁搴撲腑錛岄偅涔堜綘蹇呴』閲嶆柊寤虹珛涓涓猻ession錛屼嬌鐢ㄥ畠淇濆瓨閭d簺緇忚繃鏀瑰彉鐨勬暟鎹傚洜涓烘瘡涓疄浣撻兘鏈夎嚜宸辯殑鍞竴ID錛屽洜姝ibernate鍙互寰堝鏄撶殑鍦ㄦ柊鐨剆ession涓綆楀嚭濡備綍灝嗕復鏃跺璞¢噸鏂拌漿鎹負鐩稿簲鐨勬寔涔呭璞°?BR>

Armed with these concepts and terms, the remainder of the example is easy enough to understand. Line 31 sets up a database transaction using our open session. Within that, we create a few Track instances containing sample data and save them in the session (lines 33-50), turning them from transient instances into persistent entities. Finally, line 53 commits our transaction, atomically (as a single, indivisible unit) making all the database changes permanent. The try/catch/finally block wrapped around all this shows an important and useful idiom for working with transactions. If anything goes wrong, lines 56-60 will roll back the transaction and then bubble out the exception, leaving the database the way we found it. The session is closed in the finally portion at line 63, ensuring that this takes place whether we exit through the 'happy path' of a successful commit, or via an exception that caused rollback. Either way, it gets closed as it should.

閫氳繃涓婇潰鐨勬蹇電殑璁茶堪鍜岃璁猴紝閭d箞渚嬪瓙涓殑鍏朵粬閮ㄥ垎涔熷氨寰堝ソ鐞嗚В浜嗐傜31琛岋紝浣跨敤鎵撳紑鐨剆ession寤虹珛浜嗘暟鎹簱浜嬪姟銆傚湪浜嬪姟涓紝33錛?0琛屽垱寤轟簡涓涓猅rack瀵硅薄錛屽茍鍦╯ession涓皢鍏朵繚瀛橈紝灝嗗叾浠庝復鏃跺璞″彉涓烘寔涔呭璞°傛渶鍚庯紝53琛屾彁浜や簨鍔★紝鍘熷瓙鎬х殑鎵ц鏁版嵁搴撴敼鍙樸傝繖閲岀殑try/catch/finally灝佽浜嗕簨鍔″鐞嗕腑甯稿嚭鐜扮殑idom銆備竴鏃﹀嚭鐜頒換浣曢棶棰橈紝56錛?0琛屽氨浼氬洖婊氫簨鍔★紝鐒跺悗鎶涘嚭寮傚父錛屼繚璇佹暟鎹簱緇存寔鍘熺姸銆傛棤璁哄浣曪紝閮戒細鎵цfinnally鍧椾腑鐨?3琛岋紝鏃犺鍦ㄦ垚鍔熸墽琛屾垨鑰呭嚭鐜伴敊璇洖婊氱殑鎯呭喌涓嬶紝閮戒細鍏抽棴session銆?BR>
At the end of our method we also close the session factory itself on line 67. This is something you'd do in the 'graceful shutdown' section of your application. In a web application environment, it would be in the appropriate lifecycle event handler. [3.1] In this simple example, when the main() method returns, the application is ending.

鍦ㄦ柟娉曠殑鏈鍚?7琛岋紝灝嗕細鍏抽棴session factory銆傝繖鏄綘鐨勫簲鐢ㄧ▼搴忔甯擱鍑烘椂搴旇鎵ц鐨勬搷浣溿傚湪Web紼嬪簭涓紝浣犲垯闇瑕佷嬌鐢ㄤ竴瀹氱殑鐢熷懡鍛ㄦ湡浜嬩歡澶勭悊鍣紝瀹屾垚姝ゆ搷浣溿傚湪鎴戜滑鐨勪緥瀛愪腑錛屽湪main閫鍑虹殑鏃跺欙紝涔熷氨鏄▼搴忎腑姝㈢殑鏃墮棿銆?BR> 



]]>
hbm2java錛宻chemaexporthttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2688.htmljinfeng_wangjinfeng_wangFri, 01 Apr 2005 02:22:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2688.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2688.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/04/01/2688.html#Feedback0http://www.tkk7.com/jinfeng_wang/comments/commentRss/2688.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2688.html 

<?xml version="1.0"?>
<project name="Harnessing Hibernate: The Developer's Notebook"
         default
="db" basedir=".">

  
<!-- Set up properties containing important project directories -->
  
<property name="source.root" value="src"/>
  
<property name="class.root" value="classes"/>
  
<property name="lib.dir" value="lib"/>
  
<property name="data.dir" value="data"/>

  
<!-- Set up the class path for compilation and execution -->
  
<path id="project.class.path">
      
<!-- Include our own classes, of course -->
      
<pathelement location="${class.root}" />
      
<!-- Include jars in the project library directory -->
      
<fileset dir="${lib.dir}">
        
<include name="*.jar"/>
      
</fileset>
  
</path>

  
<target name="db" description="Runs HSQLDB database management UI
against the database file--use when application is not running"
>
      
<java classname="org.hsqldb.util.DatabaseManager"
            fork
="yes">
         
<classpath refid="project.class.path"/>
         
<arg value="-driver"/>
         
<arg value="org.hsqldb.jdbcDriver"/>
         
<arg value="-url"/>
         
<arg value="jdbc:hsqldb:${data.dir}/music"/>
         
<arg value="-user"/>
         
<arg value="sa"/>
      
</java>
  
</target>

  
<!-- Teach Ant how to use Hibernate's code generation tool -->
  
<taskdef name="hbm2java"
           classname
="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
           classpathref
="project.class.path"/>

  
<!-- Generate the java code for all mapping files in our source tree -->
  
<target name="codegen"
          description
="Generate Java source from the O/R mapping files">
    
<hbm2java output="${source.root}">
      
<fileset dir="${source.root}">
        
<include name="**/*.hbm.xml"/>
      
</fileset>
    
</hbm2java>
  
</target>

  
<!-- Create our runtime subdirectories and copy resources into them -->
  
<target name="prepare" description="Sets up build structures">
    
<mkdir dir="${class.root}"/>

    
<!-- Copy our property files and O/R mappings for use at runtime -->
    
<copy todir="${class.root}" >
      
<fileset dir="${source.root}" >
        
<include name="**/*.properties"/>
        
<include name="**/*.hbm.xml"/>
      
</fileset>
    
</copy>
  
</target>

  
<!-- Compile the java source of the project -->
  
<target name="compile" depends="prepare"
          description
="Compiles all Java classes">
    
<javac srcdir="${source.root}"
           destdir
="${class.root}"
           debug
="on"
           optimize
="off"
           deprecation
="on">
      
<classpath refid="project.class.path"/>
    
</javac>
  
</target>

  
<!-- Generate the schemas for all mapping files in our class tree -->
  
<target name="schema" depends="compile"
          description
="Generate DB schema from the O/R mapping files">

    
<!-- Teach Ant how to use Hibernate's schema generation tool -->
    
<taskdef name="schemaexport"
             classname
="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
             classpathref
="project.class.path"/>

    
<schemaexport properties="${class.root}/hibernate.properties"
                  quiet
="no" text="no" drop="no">
      
<fileset dir="${class.root}">
        
<include name="**/*.hbm.xml"/>
      
</fileset>
    
</schemaexport>
  
</target>

</project>

 

You may be wondering why the taskdef for the schema update tool is inside our schema target, rather than at the top of the build file, next to the one for hbm2java. Well, I wanted it up there too, but I ran into a snag that's worth explaining. I got strange error messages the first time I tried to build the schema target, complaining there was no hibernate.properties on the class path and our compiled Track class couldn't be found. When I ran it again, it worked. Some detective work using ant -verbose revealed that if the classes directory didn't exist when the taskdef was encountered, Ant helpfully removed it from the class path. Since a taskdef can't have its own dependencies, the solution is to move it into the schema target, giving it the benefit of that target's dependencies, ensuring the classes directory exists by the time the taskdef is processed.

涔熻浣犱細濂囨紝涓轟綍鎵句簡schema鏇存柊宸ュ叿鐨則askdef浼氬湪schema鐨則arget涓繘琛屽畾涔夛紝鑰屼笉鏄湪build鏂囦歡鐨勯《閮紝绱ф帴鐫hbm2java榪涜瀹氫箟銆傚垰寮濮嬫垜涔熸槸閭f牱鎯崇殑錛岃繖閲屾垜闇瑕佽В閲婁竴涓嬭繖閲岀殑闂銆傚綋絎竴嬈℃瀯寤簊chema鏃訛紝鎴戝緱鍒頒簡璁稿鑾悕濂囧鐨勯敊璇紝鎶ラ敊淇℃伅鏄細鍦╟lasspath涓壘涓嶅埌hibernate.properties鍜孴rack綾匯備絾鏄啀嬈℃瀯寤虹殑鏃跺欙紝灝卞彲浠ヤ簡銆傚綋浣跨敤鍛戒護鈥渁nt -verbose鈥濇椂錛屼綘灝卞彲浠ュ彂鐜板叾涓殑緇嗚妭涔嬫墍鍦ㄣ傚鏋滃湪ant瑙f瀽鍒皌askdef鏃訛紝build鏂囦歡涓墍浣跨敤鐨刢lass鐩綍騫朵笉瀛樺湪錛岄偅涔坅nt灝變細鑷繁灝嗘鐩綍浠巆lasspath涓Щ闄ゃ備絾鏄痶askdef鍙堟棤娉曞畾涔夎嚜宸辯殑dependencies錛屽洜姝よ繖閲屼笉寰椾笉灝嗗叾縐誨叆鍒皊chema target涓紝榪欐牱涔熷氨鍙互浣跨敤鍒皌arget鐨刣ependencies錛屼繚璇佸湪ant澶勭悊taskdef鐨勬椂鍊欙紝class鐩綍蹇呯劧宸茬粡瀛樺湪銆?/P>

]]>
Working with Hibernate in Eclipse - using Hibernate Synchronizer(zz)http://www.tkk7.com/jinfeng_wang/archive/2005/03/15/2084.htmljinfeng_wangjinfeng_wangTue, 15 Mar 2005 00:45:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/03/15/2084.htmlhttp://www.tkk7.com/jinfeng_wang/comments/2084.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/03/15/2084.html#Feedback1http://www.tkk7.com/jinfeng_wang/comments/commentRss/2084.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/2084.htmlhttp://www.onjava.com/pub/a/onjava/2005/01/05/hibernate.html


]]>
hibernatesynch鐨勫畨瑁呬笌浣跨敤http://www.tkk7.com/jinfeng_wang/archive/2005/02/23/1449.htmljinfeng_wangjinfeng_wangWed, 23 Feb 2005 03:03:00 GMThttp://www.tkk7.com/jinfeng_wang/archive/2005/02/23/1449.htmlhttp://www.tkk7.com/jinfeng_wang/comments/1449.htmlhttp://www.tkk7.com/jinfeng_wang/archive/2005/02/23/1449.html#Feedback4http://www.tkk7.com/jinfeng_wang/comments/commentRss/1449.htmlhttp://www.tkk7.com/jinfeng_wang/services/trackbacks/1449.html瀹夎鐨勫湪綰垮湴鍧錛?BR>         Eclipse 2.1: http://www.binamics.com/hibernatesync/eclipse2.1 
         Eclipse 3M*: http://www.binamics.com/hibernatesync

浣跨敤錛?BR>1錛夊垱寤簆roject錛岃嚜宸卞姞鍏ibernate鐨勬墍鏈夊寘錛屽綋鐒惰繕鏈夋暟鎹簱椹卞姩銆?BR>
2) 鏂板緩hibernate 鐨勯厤緗枃浠訛細File >> New >> Other >> Hibernate >> Configuration File.
     鍦ㄨ繖閲屽~鍏ラ厤緗枃浠剁殑鐩稿叧淇℃伅錛屽寘鎷細鏂囦歡鐨勫瓨鏀句綅緗紝浣犳墍浣跨敤鐨勬暟鎹簱鐨勫叿浣撻厤緗紙椹卞姩錛屽笎鍙鳳紝瀵嗙爜絳夛級銆傝繖閲岀殑session factory name 涓嶅~銆?BR>
     鍥犱負鎴戠洿鎺ユ槸搴旂敤紼嬪簭錛屾墍浠ユ病鏈変嬌鐢╠atasource銆?BR>    濡傛灉浣犱嬌鐢ㄧ殑鏄痙atasource,閭e氨闇瑕侀夋嫨application server.

    鍥犱負浠呮槸綆鍗曞簲鐢ㄧ▼搴忥紝閭d箞灝遍渶瑕佸皢閰嶇疆鏂囦歡涓殑鏈夊叧JTA鐨勯厤緗垹闄ゅ摝錛?BR>         <property name="hibernate.transaction.factory_class">
               net.sf.hibernate.transaction.JTATransactionFactory
         </property>
         <property name="jta.UserTransaction">
                  java:comp/UserTransaction
         </property>

3錛夋柊寤篽ibernate鐨刴apping鏂囦歡錛欶ile >> New >> Other >> Hibernate >> Mapping File. 
         榪欓噷闇瑕侀夋嫨mapping鏂囦歡鐨勫瓨鏀句綅緗?BR>         濉叆浣犵殑鏁版嵁搴撳笎鍙鳳紝鐒跺悗refresh錛岄夋嫨浣犵殑鏁版嵁搴撹〃銆傝繖閲岀殑table pattern,schema pattern 鑷繁鐭ラ亾鍝︺?BR>         濉啓浣犵殑鍖呭悕錛岀劧鍚庣‘璁ゃ?BR>
4錛夌敓鎴愬悇欏筳ava鏂囦歡銆?BR>        鍙抽敭鍗曞嚮mapping鏂囦歡錛岀劧鍚庨夋嫨hibernate synchronize-> synchronize files銆?榪欏氨鐢熸垚鍚勯」java鏂囦歡浜嗐?
       濡傛灉浣犵湅涓嶅埌錛岄偅灝眗efresh鏁翠釜宸ョ▼鐨勬枃浠跺す銆?BR>
5錛夌紪鍐欐祴璇曠▼搴忥紝浣跨敤閭g敓鎴愮殑java鏂囦歡榪涜鏁版嵁搴撴搷浣溿?BR>
    嫻嬭瘯紼嬪簭涓墍浣跨敤鐨勪袱涓帴鍙f槸錛歿tablename}.java  ,  {tablename}DAO.java
   渚嬪錛氬鏋滀綘鐨勮〃鍚嶆槸Person錛岄偅涔堜綘灝嗙敤Person.java, PersonDAO.java
   榪欓噷鐨凱ersonDAO灝佽浜唄ibernate鐨勬搷浣滐紝鏇村姞鏂逛究浜嗐?BR>
   鑷繁瑙傚療涓涓嬪惂錛屼細鏈夋洿澶氭敹鑾風殑銆?/P>

]]>
主站蜘蛛池模板: 美女无遮挡免费视频网站 | 免费无码又爽又刺激高潮| 久久综合给合久久国产免费| 国产日韩一区二区三免费高清| 中国一级全黄的免费观看| 在线免费观看伊人三级电影| a级毛片免费高清毛片视频| 十八禁在线观看视频播放免费| 最近免费mv在线观看动漫| 成人A片产无码免费视频在线观看| 在线播放免费人成毛片乱码| 日韩免费在线视频| 日本最新免费网站| 免费精品国产日韩热久久| 成人免费无码大片A毛片抽搐 | 国产特黄一级一片免费| 精品国产免费人成网站| 国产午夜精品免费一区二区三区 | 一区二区三区免费看| 在线观看免费黄网站| 久久综合给合久久国产免费| 国产曰批免费视频播放免费s| 国产精品美女午夜爽爽爽免费| 四虎免费在线观看| 一级毛片直播亚洲| 国产A在亚洲线播放| 亚洲永久中文字幕在线| 亚洲欧美日韩国产成人| 二级毛片免费观看全程| 久久精品国产大片免费观看| 成人免费观看一区二区| 国产在线观看免费视频播放器 | 美女视频黄的全免费视频网站| 午夜精品在线免费观看| 国产亚洲情侣一区二区无码AV| 亚洲天天在线日亚洲洲精| 亚洲色精品三区二区一区| 一级做a爱过程免费视| 91久久青青草原线免费| 国产免费午夜a无码v视频| 亚洲VA中文字幕无码一二三区 |