<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 閱讀(128) 評論(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


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲免费综合色在线视频| 亚洲AV成人无码天堂| 国产成人免费a在线视频app| 欧洲精品99毛片免费高清观看| 黄色大片免费网站| 99久久婷婷国产综合亚洲| 亚洲男人天堂av| 亚洲码国产精品高潮在线| 全部免费毛片免费播放| 在线精品免费视频| 最近2019中文字幕免费看最新| 少妇太爽了在线观看免费视频| 免费看无码特级毛片| 一级做a爱片特黄在线观看免费看| 无码天堂va亚洲va在线va| 亚洲性无码一区二区三区| 亚洲免费视频网址| 中文字幕亚洲第一在线| 久久综合九九亚洲一区| 精品亚洲永久免费精品| 最新亚洲成av人免费看| 国产亚洲欧洲Aⅴ综合一区| 亚洲v国产v天堂a无码久久| 亚洲成a人片在线观看久| 免费国产成人高清视频网站| 蜜桃精品免费久久久久影院| 性一交一乱一视频免费看| 久久精品a一国产成人免费网站 | 免费理论片51人人看电影| 免费无码肉片在线观看| 一个人看www在线高清免费看| 国产在线观看麻豆91精品免费 | 亚洲无砖砖区免费| 亚洲理论片在线观看| 亚洲欧洲日产国码www| 亚洲国产成人无码av在线播放| 亚洲国产成a人v在线| 亚洲一区二区三区播放在线| 国产亚洲中文日本不卡二区| 亚洲一区二区观看播放| 亚洲av综合日韩|