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



    主站蜘蛛池模板: 亚洲第一成年网站视频| 91亚洲导航深夜福利| 亚洲欧美精品午睡沙发| 97人妻无码一区二区精品免费| 91亚洲国产在人线播放午夜| 十八禁无码免费网站| 亚洲宅男永久在线| 免费观看91视频| 亚洲美女视频一区二区三区| 亚洲一区在线免费观看| 亚洲乱人伦精品图片| 成年性羞羞视频免费观看无限| 亚洲精品女同中文字幕| 免费久久精品国产片香蕉| 人人公开免费超级碰碰碰视频| 久久精品亚洲乱码伦伦中文| 国产成年无码久久久免费| 久久久久久亚洲精品成人| 国产在线观看片a免费观看| 亚洲精品无码少妇30P| 亚洲AV中文无码乱人伦在线视色| 国产免费久久久久久无码| 亚洲av色影在线| 成视频年人黄网站免费视频| 亚洲avav天堂av在线网毛片| 亚洲精品网站在线观看不卡无广告 | 国产精品亚洲а∨天堂2021 | 亚洲 暴爽 AV人人爽日日碰 | 久久久亚洲欧洲日产国码农村| 99在线在线视频免费视频观看 | 牛牛在线精品观看免费正 | 国产亚洲综合精品一区二区三区| 亚洲国产精品无码久久青草| 男女作爱在线播放免费网站| 亚洲人成电影青青在线播放| 又粗又硬免费毛片| 少妇无码一区二区三区免费| 噜噜综合亚洲AV中文无码| 亚洲午夜久久久影院伊人| 4虎永免费最新永久免费地址| 最好2018中文免费视频|