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

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

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

    wiflish
    Loving Life! Loving Coding!
    posts - 98,comments - 98,trackbacks - 0
    Hibernate的回調與攔截機制有三種實現方法:
    1、實體對象implements Lifecycle接口,Lifecycle接口代碼:
    public?interface?Lifecycle?{
    ????
    /**
    ?????*?在實體對象Save/Insert操作之前觸發.
    ?????
    */
    ????
    public?boolean?onSave(Session?s)?throws?CallbackException;
    ????
    ????
    /**
    ?????*?在Session.update()操作之前觸發.
    ?????
    */
    ????
    public?boolean?onUpdate(Session?s)?throws?CallbackException;

    ????
    /**
    ?????*?在實體對象刪除之前觸發.
    ?????
    */
    ????
    public?boolean?onDelete(Session?s)?throws?CallbackException;

    ????
    /**
    ???? *?在實體對象加載之后觸發.
    ?????
    */
    ????
    public?void?onLoad(Session?s,?Serializable?id);
    }
    實體對象通過實現Lifecycle接口,即可以在特定的持久化階段,觸發特定的處理過程。比如在實體對象Tuser實現了Lifecycle接口的onSave方法,則在實體對象Tuser保存之前將先執行onSave方法。

    2、實體對象implements Validatable接口,Validatable接口代碼:
    public?interface?Validatable?{
    ????
    public?void?validate()?throws ValidationFailure;
    }
    ?? Validatable接口定義了數據驗證實現方式。實體對象實現Validatable接口,并在validate方法中對當前待保存的數據進行驗證,以保證數據的邏輯合法性(由于該方法在實體對象生命周期內,可能被多次調用,所以此方法最好只用于數據本身的邏輯合法性驗證,而不要試圖去實現數據業務邏輯的驗證)。

    以上2種方法都要求實現Hibernate中的Lifecycle或Validatable接口,具有很大的侵入性,使得實體對象的移植很不方便。Hibernate又提供了一種攔截機制,為對象持久化事件的捕獲和處理提供了一個非侵入性的實現。

    3、實現Interceptor接口,在創建session時,指定加載Interceptor相應的實現類,此session 的持久化操作都將首先經由此攔截器捕獲處理。Interceptor(Hibernate3)接口代碼:
    package?org.hibernate;

    import?java.io.Serializable;
    import?java.util.Iterator;

    import?org.hibernate.type.Type;

    public?interface?Interceptor?{
    ?
    ???
    //對象初始化之前加載,這里的entity處于剛被創建的狀態(即屬性均未賦值).
    ????
    public?boolean?onLoad(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,
    ??????????? Type[]?types)?
    throws?CallbackException;
    ???

    ??? //Session.flush()方法進行臟數據檢查時,如果發現PO狀態改變,則調用此方法(即實體對象更新之前調用).
    ????public?boolean?onFlushDirty(Object?entity,?Serializable?id,?Object[]?currentState,
    ?????????? Object[]?previousState,?String[]?propertyNames,?Type[]?types)?
    throws?CallbackException;
    ???
    ??? //在實體對象被保存之前調用.
    ????
    public?boolean?onSave(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,
    ?????????? Type[]?types)?
    throws?CallbackException;
    ???

    ??? //在對象被刪除之前調用.
    ????public?void?onDelete(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,
    ?????????? Type[]?types)?
    throws?CallbackException;
    ????
    /**
    ?????*?Called?before?a?collection?is?(re)created.
    ?????
    */
    ????
    public?void?onCollectionRecreate(Object?collection,?Serializable?key)?throws?CallbackException;
    ????
    /**
    ?????*?Called?before?a?collection?is?deleted.
    ?????
    */
    ????
    public?void?onCollectionRemove(Object?collection,?Serializable?key)?throws?CallbackException;
    ????
    /**
    ?????*?Called?before?a?collection?is?updated.
    ?????
    */
    ????
    public?void?onCollectionUpdate(Object?collection,?Serializable?key)?throws?CallbackException;
    ???

    ??? //Session執行flush方法之前調用.
    ????public?void?preFlush(Iterator?entities)?throws?CallbackException;
    ??

    ??? //Session執行flush方法之后調用.
    ????public?void?postFlush(Iterator?entities)?throws?CallbackException;

    ????
    /**
    ?????*?Called?to?distinguish?between?transient?and?detached?entities.?The?return?value?determines?the
    ?????*?state?of?the?entity?with?respect?to?the?current?session.
    ?????*?<ul>
    ?????*?<li><tt>Boolean.TRUE</tt>?-?the?entity?is?transient
    ?????*?<li><tt>Boolean.FALSE</tt>?-?the?entity?is?detached
    ?????*?<li><tt>null</tt>?-?Hibernate?uses?the?<tt>unsaved-value</tt>?mapping?and?other?heuristics?to?
    ?????*?determine?if?the?object?is?unsaved
    ?????*?</ul>
    ?????*?
    @param?entity?a?transient?or?detached?entity
    ?????*?
    @return?Boolean?or?<tt>null</tt>?to?choose?default?behaviour
    ?????
    */
    ????
    public?Boolean?isTransient(Object?entity);
    ????
    /**
    ?????*?Called?from?<tt>flush()</tt>.?The?return?value?determines?whether?the?entity?is?updated
    ?????*?<ul>
    ?????*?<li>an?array?of?property?indices?-?the?entity?is?dirty
    ?????*?<li>an?empty?array?-?the?entity?is?not?dirty
    ?????*?<li><tt>null</tt>?-?use?Hibernate's?default?dirty-checking?algorithm
    ?????*?</ul>
    ?????*?
    @param?entity?a?persistent?entity
    ?????*?
    @return?array?of?dirty?property?indices?or?<tt>null</tt>?to?choose?default?behaviour
    ?????
    */
    ????
    public?int[]?findDirty(Object?entity,?Serializable?id,?Object[]?currentState,
    ???????????? Object[]?previousState, String[]?propertyNames,?Type[]?types);
    ????
    /**
    ?????*?Instantiate?the?entity?class.?Return?<tt>null</tt>?to?indicate?that?Hibernate?should?use
    ?????*?the?default?constructor?of?the?class.?The?identifier?property?of?the?returned?instance
    ?????*?should?be?initialized?with?the?given?identifier.
    ?????*
    ?????*?
    @param?entityName?the?name?of?the?entity
    ?????*?
    @param?entityMode?The?type?of?entity?instance?to?be?returned.
    ?????*?
    @param?id?the?identifier?of?the?new?instance
    ?????*?
    @return?an?instance?of?the?class,?or?<tt>null</tt>?to?choose?default?behaviour
    ?????
    */
    ????
    public?Object?instantiate(String?entityName,?EntityMode?entityMode,
    ?????????? Serializable?id)?
    throws?CallbackException;

    ????
    /**
    ?????*?Get?the?entity?name?for?a?persistent?or?transient?instance
    ?????*?
    @param?object?an?entity?instance
    ?????*?
    @return?the?name?of?the?entity
    ?????
    */
    ????
    public?String?getEntityName(Object?object)?throws?CallbackException;

    ????
    /**
    ?????*?Get?a?fully?loaded?entity?instance?that?is?cached?externally
    ?????*?
    @param?entityName?the?name?of?the?entity
    ?????*?
    @param?id?the?instance?identifier
    ?????*?
    @return?a?fully?initialized?entity
    ?????*?
    @throws?CallbackException
    ?????
    */
    ????
    public?Object?getEntity(String?entityName,?Serializable?id)?throws?CallbackException;
    ????
    ????
    /**
    ?????*?Called?when?a?Hibernate?transaction?is?begun?via?the?Hibernate?<tt>Transaction</tt>?
    ?????*?API.?Will?not?be?called?if?transactions?are?being?controlled?via?some?other?
    ?????*?mechanism?(CMT,?for?example).
    ?????
    */
    ????
    public?void?afterTransactionBegin(Transaction?tx);
    ????
    /**
    ?????*?Called?before?a?transaction?is?committed?(but?not?before?rollback).
    ?????
    */
    ????
    public?void?beforeTransactionCompletion(Transaction?tx);
    ????
    /**???? *?Called?after?a?transaction?is?committed?or?rolled?back.
    ?????
    */
    ????
    public?void?afterTransactionCompletion(Transaction?tx);

    ????
    /**
    ?????*?Called?when?sql?string?is?being?prepared.?
    ?????*?
    @param?sql?sql?to?be?prepared
    ?????*?
    @return?original?or?modified?sql
    ?????
    */
    ????
    public?String?onPrepareStatement(String?sql);
    }




    posted on 2006-08-16 17:41 想飛的魚 閱讀(1216) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 国产日韩AV免费无码一区二区| 1000部拍拍拍18勿入免费凤凰福利| 日本精品人妻无码免费大全| 亚洲黄色免费观看| 四虎国产成人永久精品免费| 亚洲成人午夜在线| 99re6免费视频| 亚洲1234区乱码| 成人性生交大片免费看午夜a| 亚洲AV无码资源在线观看| 国产免费拔擦拔擦8x| 一级毛片a免费播放王色电影 | 国产色爽免费无码视频| 久久久无码精品亚洲日韩按摩 | 国产精品亚洲四区在线观看 | 亚洲成a人片在线观看播放| 亚洲高清中文字幕免费| 亚洲日产乱码一二三区别 | 免费人成年激情视频在线观看 | 亚洲av无码专区亚洲av不卡| 国产免费直播在线观看视频| 男女一边桶一边摸一边脱视频免费 | 久久久久亚洲AV无码网站| 97热久久免费频精品99| 亚洲精品理论电影在线观看| 精品亚洲视频在线观看 | 无遮挡呻吟娇喘视频免费播放| 亚洲精品无码鲁网中文电影| 国产在线观看麻豆91精品免费| 久久无码av亚洲精品色午夜| 国精无码欧精品亚洲一区| 无人在线直播免费观看| 搜日本一区二区三区免费高清视频 | 国产h视频在线观看网站免费| 亚洲av无码片vr一区二区三区| 亚洲一区二区三区AV无码| 老司机在线免费视频| 国产A∨免费精品视频| 亚洲剧场午夜在线观看| 亚洲中文字幕无码一久久区| 男女免费观看在线爽爽爽视频|