Hibernate學習---第十三節:hibernate過濾器和攔截器的實現

來源:互聯網
上載者:User

標籤:

一、hibernate 過濾器

1、在持久化對應檔中配置過濾器,代碼如下:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="learn.hibernate.bean">    <!-- 定義過濾器,並指定參數 -->    <filter-def name="password_filter">        <filter-param name="ps" type="integer"/>        <filter-param name="ps2" type="integer"/>    </filter-def>        <class name="Person" table="t_person">        <id name="id" column="person_id">            <generator class="native"/>        </id>        <property name="name" column="t_name"/>        <property name="age"/>            <property name="passwork"/>        <property name="birthday"/>        <filter name="password_filter" condition="password between :ps and :ps2"/>    </class></hibernate-mapping>

2、測試過濾器:

@Testpublic void testQuery(){    // 啟用指定名稱對應的過濾器    Filter filter = session.enableFilter("password_filter");    // 過濾器參數綁定(過濾條件的綁定)    filter.setParameter("ps", 123458);    filter.setParameter("ps2", 123464);    Query query = session.createQuery("from Person");    List<Person> list = query.list();    for(Person p : list){        System.out.println(p);    }        System.out.println("--------------------------");    // 停用指定的過濾器    // 如果在一個有開啟開啟過濾器的 session 中進行對應的持久化類的查詢操作都會自動加上過濾條件    // session.disableFilter("password_filter");    Query query2 = session.createQuery("from Person where id in (:ids)");    query2.setParameter("ids", new Object[]{4,5});    List<Person> list2 = query2.list();    for(Person p : list2){        System.out.println(p);    }        System.out.println("--------------------------");    // 通過以下實驗證明過濾器只是加在 create* 方法上    session.clear();    Person person = (Person)session.get(Person.class, 6);    System.out.println(person);}

二、hibernate 攔截器

1、寫一個繼承 EmptyInterceptor 類實現相關的方法,代碼如下:

package learn.hibernate.interceptor;import java.io.Serializable;import java.util.Arrays;import java.util.Date;import learn.hibernate.bean.Person;import org.hibernate.EmptyInterceptor;import org.hibernate.type.Type;/** * 自訂攔截器 * 在執行某個動作之前或之後加一個自己的業務操作 * @author Administrator * */public class MyInterceptor extends EmptyInterceptor{    @Override    public boolean onSave(Object entity, Serializable id, Object[] state,            String[] propertyNames, Type[] types) {        System.out.println("-------------攔截儲存-------------");        System.out.println("entity:"+entity);        System.out.println("id:"+id);        System.out.println("state:"+Arrays.toString(state));        System.out.println("propertyNames:"+Arrays.toString(propertyNames));        System.out.println("types:"+Arrays.toString(types));                if(entity.getClass() == Person.class){            for(int i = 0, len = propertyNames.length; i < len; i++){                if(propertyNames[i].equals("birthday")){                    if(state[i]==null){                        Person p = (Person) entity;                        p.setBirthday(new Date());                    }                }            }        }                return super.onSave(entity, id, state, propertyNames, types);    }}

以上的類實現了 EmptyInterceptor 類的 onSave() 方法,也就是說在儲存資料的時候調用該方法可做相關的攔截

2、測試類別代碼如下:

/** * 批量寫入資料 */@Testpublic void testAdd(){    // 開啟攔截器(局部攔截器)    session = factory.withOptions().interceptor(new MyInterceptor()).openSession();    tx = session.beginTransaction();        Person person = new Person("admin", 22, 123456, null);    session.persist(person);        tx.commit();}

Hibernate學習---第十三節:hibernate過濾器和攔截器的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.