<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的回調(diào)與攔截機(jī)制有三種實(shí)現(xiàn)方法:
    1、實(shí)體對象implements Lifecycle接口,Lifecycle接口代碼:
    public?interface?Lifecycle?{
    ????
    /**
    ?????*?在實(shí)體對象Save/Insert操作之前觸發(fā).
    ?????
    */
    ????
    public?boolean?onSave(Session?s)?throws?CallbackException;
    ????
    ????
    /**
    ?????*?在Session.update()操作之前觸發(fā).
    ?????
    */
    ????
    public?boolean?onUpdate(Session?s)?throws?CallbackException;

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

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

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

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

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

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

    import?org.hibernate.type.Type;

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

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

    ??? //在對象被刪除之前調(diào)用.
    ????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執(zhí)行flush方法之前調(diào)用.
    ????public?void?preFlush(Iterator?entities)?throws?CallbackException;
    ??

    ??? //Session執(zhí)行flush方法之后調(diào)用.
    ????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 想飛的魚 閱讀(1231) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 亚洲А∨精品天堂在线| 免费黄色一级毛片| 亚洲午夜福利在线观看| 香蕉视频免费在线| 国产成人精品男人免费| 美美女高清毛片视频黄的一免费| 韩国二级毛片免费播放| 国产亚洲综合一区二区三区| 四虎永久成人免费| 日日躁狠狠躁狠狠爱免费视频| 亚洲国产高清精品线久久| 国产成人无码精品久久久免费| 亚洲综合av永久无码精品一区二区 | 曰批免费视频播放免费| 国产成人亚洲精品91专区手机| 国产黄在线播放免费观看| 国产精品亚洲综合一区| 一个人免费视频观看在线www| 亚洲av日韩av不卡在线观看| 99ee6热久久免费精品6| 亚洲日韩国产精品乱-久| 日本一道一区二区免费看| 一个人看www免费高清字幕| 国产精品亚洲аv无码播放| **实干一级毛片aa免费| 亚洲一区二区三区在线观看蜜桃| 午夜免费福利在线| 国产黄在线播放免费观看| 666精品国产精品亚洲| 精品久久洲久久久久护士免费| 国产亚洲漂亮白嫩美女在线| 亚洲区小说区图片区QVOD| 人与禽交免费网站视频| 日韩色日韩视频亚洲网站| 亚洲人成伊人成综合网久久久| 69式互添免费视频| 美女黄色毛片免费看| 亚洲国产人成网站在线电影动漫 | 在线观看亚洲人成网站| 国产精品久久免费视频| 久久国产免费观看精品|