<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Neil的備忘錄

    just do it
    posts - 66, comments - 8, trackbacks - 0, articles - 0

    Transitive persistence

    Posted on 2008-11-01 10:11 Neil's NoteBook 閱讀(129) 評(píng)論(0)  編輯  收藏

    It is quite cumbersome to save, delete, or reattach individual objects, especially if you deal with a graph of associated objects. A common case is a parent/child relationship. Consider the following example:

    If the children in a parent/child relationship would be value typed (e.g. a collection of addresses or strings), their life cycle would depend on the parent and no further action would be required for convenient "cascading" of state changes. When the parent is saved, the value-typed child objects are saved as well, when the parent is deleted, the children will be deleted, etc. This even works for operations such as the removal of a child from the collection; Hibernate will detect this and, since value-typed objects can't have shared references, delete the child from the database.

    Now consider the same scenario with parent and child objects being entities, not value-types (e.g. categories and items, or parent and child cats). Entities have their own life cycle, support shared references (so removing an entity from the collection does not mean it can be deleted), and there is by default no cascading of state from one entity to any other associated entities. Hibernate does not implement persistence by reachability by default.

    For each basic operation of the Hibernate session - including persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(), evict(), replicate() - there is a corresponding cascade style. Respectively, the cascade styles are named create, merge, save-update, delete, lock, refresh, evict, replicate. If you want an operation to be cascaded along an association, you must indicate that in the mapping document. For example:

    <one-to-one name="person" cascade="persist"/>

    Cascade styles my be combined:

    <one-to-one name="person" cascade="persist,delete,lock"/>

    You may even use cascade="all" to specify that all operations should be cascaded along the association. The default cascade="none" specifies that no operations are to be cascaded.

    A special cascade style, delete-orphan, applies only to one-to-many associations, and indicates that the delete() operation should be applied to any child object that is removed from the association.

    Recommendations:

    • It doesn't usually make sense to enable cascade on a <many-to-one> or <many-to-many> association. Cascade is often useful for <one-to-one> and <one-to-many> associations.

    • If the child object's lifespan is bounded by the lifespan of the parent object, make it a life cycle object by specifying cascade="all,delete-orphan".

    • Otherwise, you might not need cascade at all. But if you think that you will often be working with the parent and children together in the same transaction, and you want to save yourself some typing, consider using cascade="persist,merge,save-update".

    Mapping an association (either a single valued association, or a collection) with cascade="all" marks the association as a parent/child style relationship where save/update/delete of the parent results in save/update/delete of the child or children.

    Futhermore, a mere reference to a child from a persistent parent will result in save/update of the child. This metaphor is incomplete, however. A child which becomes unreferenced by its parent is not automatically deleted, except in the case of a <one-to-many> association mapped with cascade="delete-orphan". The precise semantics of cascading operations for a parent/child relationship are as follows:

    • If a parent is passed to persist(), all children are passed to persist()

    • If a parent is passed to merge(), all children are passed to merge()

    • If a parent is passed to save(), update() or saveOrUpdate(), all children are passed to saveOrUpdate()

    • If a transient or detached child becomes referenced by a persistent parent, it is passed to saveOrUpdate()

    • If a parent is deleted, all children are passed to delete()

    • If a child is dereferenced by a persistent parent, nothing special happens - the application should explicitly delete the child if necessary - unless cascade="delete-orphan", in which case the "orphaned" child is deleted.

    Finally, note that cascading of operations can be applied to an object graph at call time or at flush time. All operations, if enabled, are cascaded to associated entities reachable when the operation is executed. However, save-upate and delete-orphan are transitive for all associated entities reachable during flush of the Session


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 精品国产污污免费网站入口 | 91免费国产在线观看| 久久久精品国产亚洲成人满18免费网站| 亚洲性线免费观看视频成熟| 国产精品怡红院永久免费| 亚洲国产日韩在线一区| 四虎国产精品免费久久| 99亚偷拍自图区亚洲| 国产网站在线免费观看| 免费夜色污私人影院网站| 亚洲国产专区一区| 免费无码又爽又刺激网站| 久久久久久a亚洲欧洲AV| 91禁漫免费进入| 亚洲人成7777影视在线观看| 全免费a级毛片免费看不卡| 另类图片亚洲校园小说区| 亚洲一区二区高清| 久久国产免费观看精品3| 亚洲另类春色校园小说| 永久免费毛片手机版在线看| 免费视频精品一区二区| 亚洲AV日韩AV天堂一区二区三区| 最近最新高清免费中文字幕| 一级特黄录像免费播放中文版| 亚洲精品第一国产综合境外资源 | 一级毛片免费毛片一级毛片免费| 亚洲欧洲校园自拍都市| 最近最好的中文字幕2019免费| 深夜福利在线视频免费| 亚洲AV人无码综合在线观看| 大学生美女毛片免费视频| 一级毛片免费全部播放| 亚洲国产精品线观看不卡| 免费乱理伦在线播放| 无码av免费网站| 亚洲丶国产丶欧美一区二区三区| 久久精品国产精品亚洲| 18禁网站免费无遮挡无码中文| 免费人成网站永久| 亚洲一区二区三区91|