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

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

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

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    hbn 攔截器

    Posted on 2007-11-29 10:13 G_G 閱讀(1029) 評論(0)  編輯  收藏 所屬分類: hibernate
    攔截器
    package ?hbn.test.supper.Interceptor;

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

    import ?org.hibernate.CallbackException;
    import ?org.hibernate.EntityMode;
    import ?org.hibernate.Interceptor;
    import ?org.hibernate.Transaction;
    import ?org.hibernate.type.Type;

    public ? class ?TestInterceptor? implements ?Interceptor,Serializable{

    ????
    private ?Set?inserts? = ? new ?HashSet();
    ????
    private ?Set?updates? = ? new ?HashSet();
    ????
    ?? ?
    // Session初化一個持久對象 如果這方法中改變了對象屬性就返回true 否則null
    ???? public ? boolean ?onLoad(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
    ????
    ??? return ? false ;
    ????}
    ??? // Session flush()中檢查到臟數據是調用 如:tr.commit() ....
    ???? public ? boolean ?onFlushDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
    ????????updates.add(entity);
    ????????
    return ? false ;
    ????}
    ???
    // Session Save() 當修改了對象屬性返回true
    ???? public ? boolean ?onSave(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
    ????????
    ????????inserts.add(entity);
    ????????
    return ? false ;
    ????}
    ????
    // delete
    ???? public ? void ?onDelete(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
    ????}
    ? ?? //flush() 之前調用
    ???? public ? void ?preFlush(Iterator?entities)? throws ?CallbackException?{
    ????}
    ????
    // flush() 執行SQL語句之后調用
    ???? public ? void ?postFlush(Iterator?entities)? throws ?CallbackException?{
    ????????
    ????????
    try ?{
    ????????????
    for (Iterator?it? = ?updates.iterator();it.hasNext();){
    ????????????????System.out.println(
    " update= " + ?it.next()?);????
    ????????????}
    ????????????
    for (Iterator?it? = ?inserts.iterator();it.hasNext();){
    ????????????????System.out.println(
    " insert " + ?it.next()?);????
    ????????????}
    ????????????
    ????????}?
    catch ?(Exception?e)?{
    ????????????e.printStackTrace();
    ????????}
    ????????
    ????}

    ????
    public ?Boolean?isTransient(Object?entity)?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ???????? return ? null ;
    ????}
    ? ? //決定Session中那些對象是臟數據 如果null Session使用默認處理臟數據
    ???? public ? int []?findDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)?{
    ???????? return ? null ;
    ????}

    ????
    // 當Session構造實體類對象前調用
    ???? public ?Object?instantiate(String?entityName,?EntityMode?entityMode,?Serializable?id)? throws ?CallbackException?{
    ???????? return ? null ;
    ????}

    ????
    public ?String?getEntityName(Object?object)? throws ?CallbackException?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ???????? return ? null ;
    ????}

    ????
    public ?Object?getEntity(String?entityName,?Serializable?id)? throws ?CallbackException?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ???????? return ? null ;
    ????}

    ????
    public ? void ?afterTransactionBegin(Transaction?tx)?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ????????
    ????}

    ????
    public ? void ?beforeTransactionCompletion(Transaction?tx)?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ????????
    ????}

    ????
    public ? void ?afterTransactionCompletion(Transaction?tx)?{
    ????????
    // ?TODO?Auto-generated?method?stub
    ????????
    ????}

    }

    測試

    package ?hbn.test.supper.Interceptor;

    import ?java.lang.reflect.Field;

    import ?org.hibernate.Session;
    import ?org.hibernate.SessionFactory;
    import ?org.hibernate.Transaction;

    import ?hbn.HibernateSessionFactory;
    import ?hbn.bean.T2oo;
    import ?junit.framework.TestCase;

    public ? class ?TestIC? extends ?TestCase?{
    ????
    private ?SessionFactory?sessionFactory;
    ????
    protected ? void ?setUp()? throws ?Exception?{
    ????????
    super .setUp();
    ????????
    // 利用java反射得到?HibernateSessionFactory?->
    ????????
    // private??static?org.hibernate.SessionFactory?sessionFactory;
    ????????
    // 要模擬?并發?要?HibernateSessionFactory?得出的?有?threadLocal?不行?
    ????????HibernateSessionFactory.currentSession();
    ????????HibernateSessionFactory.closeSession();
    ????????Field?field?
    = ?HibernateSessionFactory. class .getDeclaredField( " sessionFactory " );
    ????????field.setAccessible(
    true );
    ????????sessionFactory?
    = ?(SessionFactory)?field.get(HibernateSessionFactory. class );
    ????}
    ????
    ????
    public ? void ?testInc()? throws ?Exception?{
    ????????TestInterceptor?intx?
    = ? new ?TestInterceptor();
    ????????
    // 加載攔截器
    ????????Session?session? = ?sessionFactory.openSession(intx);
    ????????
    ????????Transaction?tr?
    = ?session.beginTransaction();
    ????????T2oo?t2?
    = ? new ?T2oo( 23 );
    ????????session.save(t2);
    ????????t2.setAvg(
    new ?Integer( 99 ));
    ????????tr.commit();
    ????}
    }
    結果
    Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
    Hibernate: update t2oo set version=?, avg=?, aid=? where id=? and version=?
    //攔截到的
    update=hbn.bean.T2oo@277
    inserthbn.bean.T2oo@277



    主站蜘蛛池模板: 中文字幕一区二区三区免费视频| 亚洲国产一区二区三区青草影视| 青青操免费在线观看| 亚洲日韩中文字幕无码一区| 亚洲一区二区中文| 国产自偷亚洲精品页65页| 毛片免费在线观看网站| 99久久免费精品高清特色大片| kk4kk免费视频毛片| 国产亚洲女在线线精品| 亚洲综合在线一区二区三区| 亚洲人成777在线播放| 亚洲日韩中文字幕天堂不卡| 久久水蜜桃亚洲av无码精品麻豆| 亚洲综合色丁香婷婷六月图片| 久久亚洲精品成人无码网站| 亚洲av日韩av天堂影片精品| 国产av天堂亚洲国产av天堂| 亚洲精品无码Av人在线观看国产| 亚洲中文字幕无码永久在线| 亚洲国产精品综合久久网各| 亚洲成熟丰满熟妇高潮XXXXX| 狠狠入ady亚洲精品| 一级做a爱过程免费视| 国产精品免费久久久久电影网| 99热在线观看免费| 天堂亚洲免费视频| 亚洲成人高清在线| 伊伊人成亚洲综合人网7777| 亚洲国产精品xo在线观看| www.av在线免费观看| 国产一卡2卡3卡4卡无卡免费视频| 免费看韩国黄a片在线观看| 又大又黄又粗又爽的免费视频| 亚洲日韩人妻第一页| 久久久久亚洲AV片无码下载蜜桃 | 最近中文字幕电影大全免费版 | 中国亚洲呦女专区| 日本免费一区二区三区四区五六区 | 亚洲精品无码久久久久| 亚洲欧美在线x视频|