<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)航:
     
    主站蜘蛛池模板: 亚洲中文字幕不卡无码| 国产AV无码专区亚洲AWWW| 成年大片免费视频播放一级| 亚洲精品中文字幕无码蜜桃| 亚洲精品免费在线视频| 亚洲成a人片在线不卡一二三区 | 亚洲国产精品无码成人片久久| 91人成网站色www免费下载| 亚洲日韩国产欧美一区二区三区| 亚洲一级片内射网站在线观看| 91久久青青草原线免费| 羞羞网站在线免费观看| 亚洲天堂男人天堂| 日韩精品免费电影| 未满十八18禁止免费无码网站| 亚洲精品天堂在线观看| 亚洲va久久久噜噜噜久久狠狠| 毛片免费视频播放| a在线视频免费观看| 亚洲精品无码人妻无码| 亚洲日本一区二区三区| 久久影视国产亚洲| 成人免费看片又大又黄| 久久不见久久见免费视频7 | 国产免费一区二区三区| 成人午夜影视全部免费看| 亚洲AV成人噜噜无码网站| 日本亚洲欧洲免费天堂午夜看片女人员| 日韩高清在线免费观看| 日本免费网站视频www区| 国产精品综合专区中文字幕免费播放| 亚洲综合av一区二区三区不卡 | 国产区图片区小说区亚洲区| 亚洲最新黄色网址| 亚洲香蕉成人AV网站在线观看| 日本无卡码免费一区二区三区| 无码区日韩特区永久免费系列 | 亚洲男人av香蕉爽爽爽爽| 丁香花在线观看免费观看| 久久aⅴ免费观看| 久久久久久国产a免费观看不卡 |