Hibernate3高级特性-使用过滤器

转  http://www.cnblogs.com/yql1986/archive/2011/10/14/2210568.html


  Hibernate3 新增了对某个类或集合使用预定义的过滤器条件 (filter criteria) 的功能。过滤器条件相当于定义一个非常类似于类和各种集合上的"where"属性的约束子句,但是过滤器条件可以带参数。应用程序可以在运行时决定是否启用给定的过滤器,以及使用什么样的参数值。过滤器的用法很像数据库视图,只还过是在应用程序员中确定使用什么样的参数。摘自--

http://docs.jboss.org/hibernate/core/3.6/reference/zh-CN/html/filters.html

一、在Hibernate中使用过滤器遵循以下步骤

  1. Define the filter within the mapping file of the targeted entity  (identity the attributes to filter on,and their types)
  2. Apply the  filter on the desired class or collection by indicating it with the <class> or <collection-type> tags
  3. After obtaining a session with which to perform your actions,enable the appropriate filter , setting any applicable parameters

二 使用 Hibernate3 过滤器

 

 工程结构图(1)

在下面的两个例子中,不贴出 hibernate.cfg.xml和log4j.xml 两个配置文件的代码

example 1:  Customer Class Filter Test

    假设通过指定顾客的年龄作为查询条件,获取符合查询条件的顾客

1。Customer类文件,Customer 的属性分别是:年龄和姓名 并且每个Customer均有各自的id号

   Customer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.laoyangx.chapter0;
 
import java.io.Serializable;
 
public class Customer implements Serializable {
 
     private static final long serialVersionUID = 9210200371341904932L;
     private Integer id; //顾客编号
     private Integer age;  //顾客年龄
     private String name; //顾客姓名
     
     public Customer() {
         
     }
     
     public Customer(Integer age, String name) {
         
         this .age = age;
         this .name = name;
     }
     public Integer getId() {
         return id;
     }
     public void setId(Integer id) {
         this .id = id;
     }
     public Integer getAge() {
         return age;
     }
     public void setAge(Integer age) {
         this .age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this .name = name;
     }
         
}

2。 Hibernate的工具类

HibernateUtil.java

package com.laoyangx.chapter0;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
@SuppressWarnings ({ "rawtypes" , "unchecked" })
public class HibernateUtil {
     private static final SessionFactory sessionFactory;
     private static final ThreadLocal localSession = new ThreadLocal();
     
     static {
         try {
             sessionFactory = new Configuration().configure().buildSessionFactory();
         } catch (HibernateException ex){
             throw new RuntimeException( "sessionFactory :" + ex.getMessage(), ex);
         }
     }
     
     public static Session currentSession() throws HibernateException {
         Session s = (Session) localSession.get();
         if (s == null ) {
             s = sessionFactory.openSession();
             localSession.set(s);
         }
         return s;
     }
     
     public static void closeSession() throws HibernateException {
         Session s = (Session) localSession.get();
         localSession.set( null );
         if (s != null )
             s.close();
     }
}

3。Customer类的数据库映射文件,在配置文件增加了<filter-def>和<filter>两个元素,过滤条件是:顾客年龄大于25

  chapter0_customer.hbm.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6   <class name="com.laoyangx.chapter0.Customer" table="T_chapter0_customers" >
 7       <id name="id" column="customer_id" type="java.lang.Integer">
 8          <generator class="native"/>
 9       </id>
10      <property name="age" column="customer_age" type="java.lang.Integer" />
11      <property name="name" column="customer_name" type="java.lang.String" length="25" />
12      <filter name="creationAgeFilter" condition="customer_age>:asOfAge" />
13   </class>
14   <filter-def name="creationAgeFilter">
15     <filter-param name="asOfAge" type="java.lang.Integer"/>
16   </filter-def>
17 </hibernate-mapping>
复制代码

4。测试
  TestFilterDef.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.laoyangx.chapter0;
 
import java.util.Iterator;
import java.util.List;
 
import junit.framework.TestCase;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
public class TestFilterDef extends TestCase {
     
     /**
      * 添加顾客
      */
     public void addCustomer(){
         
         Session session=HibernateUtil.currentSession();
         Transaction transaction= null ;
         
         try {
             transaction=session.beginTransaction();
             
             for ( int i= 20 ;i< 30 ;i++){
 
                 session.save( new Customer(i, "customer" +i));
             }
             
             transaction.commit();
         } catch (HibernateException e) {
             
             if (transaction!= null ) transaction.rollback();
             
             e.printStackTrace();
         }
         finally {
             session.close();
         }      
     }
     
     
     /**
      * 通过指定顾客的年龄作为查询条件,获取顾客
      */
     public void getCustomerWithAge(){
         
         Session session=HibernateUtil.currentSession();
         Transaction transaciton= null ;
         
         try {
             
             transaciton=session.beginTransaction();
             session.enableFilter( "creationAgeFilter" ).setParameter( "asOfAge" , new Integer( 25 )); //顾客年龄大于25
             
             List resultSet=(List)session.createQuery( "from Customer" ).list();
             boolean resultNotEmpty=(resultSet!= null )&&resultSet.size()> 0 ;
             if (resultNotEmpty){
                 for (Iterator iter=resultSet.iterator();iter.hasNext();){
                     Customer customer=(Customer)iter.next();
                     
                     System.out.println( "customer_id-> " +customer.getId()+ " customer_name-> " +customer.getName()
                             + " customer_age-> " +customer.getAge());
                 }
             }
                                 
         } catch (HibernateException e) {
            
             if (transaciton!= null ) transaciton.rollback();
                 
             e.printStackTrace();
         }
         finally {
             session.close();
         }
         
     }  
}

先运行 addCustomer()方法,向数据库中添加测试数据 运行之后,T_chapter0_customers表中的数据显示如下:

随后执行 getCustomerWithAge() 显示结果如下:

example 2: Customer Collection Filter

假设 Customer 与 Order 是一对多的关系 , 每个 Customer可以有多个 Order

1。Customer类文件,Customer 的属性分别是:年龄和姓名 并且每个Customer均有各自的id号。Order类文件 , Order 的属性分别是:日期、是否可见和订单编号。Customer 与 Order 二者之间通过 customer_order_id 进行关联。

Customer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.laoyangx.chapter0;
 
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
 
public class Customer implements Serializable {
 
     private static final long serialVersionUID = 9210200371341904932L;
     private Integer id;   //顾客编号
     private Integer age;  //顾客年龄
     private String name;  //顾客姓名
     private Set<Order> orders= new HashSet<Order>();
     
     public Customer() { }
     
     public Customer(Integer age, String name) {
         
         this .age = age;
         this .name = name;
     }
     public Integer getId() {
         return id;
     }
     public void setId(Integer id) {
         this .id = id;
     }
     public Integer getAge() {
         return age;
     }
     public void setAge(Integer age) {
         this .age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this .name = name;
     }
 
     public Set<Order> getOrders() {
         return orders;
     }
 
     public void setOrders(Set<Order> orders) {
         this .orders = orders;
     }      
}

Order.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.laoyangx.chapter0;
 
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Order implements Serializable {
 
     private static final long serialVersionUID = 5266816497026247617L;
     private Integer id;  //订单编号
     private String date; //订单日期
     private Boolean visible; //订单状态
     private Customer customer; //订单来源
     
     public Order() {}
     
     public Order(Date date, Customer customer,Boolean visible) {
         
         SimpleDateFormat sdf= new SimpleDateFormat( "yyyy-MM-dd" );
         this .date = sdf.format(date);
         this .customer = customer;
         this .visible=visible;
     }
     
     public Integer getId() {
         return id;
     }
 
     public void setId(Integer id) {
         this .id = id;
     }
 
     public String getDate() {
         return date;
     }
 
     public void setDate(String date) {           
       this .date =date;
     }
 
     public Customer getCustomer() {
         return customer;
     }
 
     public void setCustomer(Customer customer) {
         this .customer = customer;
     }
 
     public Boolean getVisible() {
         return visible;
     }
 
     public void setVisible(Boolean visible) {
         this .visible = visible;
     }
     
}

2。HibernateUtil工具类

       HibbernateUitl.java 和 example1 中的 HibernateUitl.java 一样

3。Customer类的数据库映射文件,在配置文件增加 两个<filter-def>和<filter>,过滤条件是:订单的visible属性为true且顾客年龄大于25

chapter0_customer.hbm.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6   <class name="com.laoyangx.chapter0.Customer" table="T_chapter0_customers" >
 7       <id name="id" column="customer_id" type="java.lang.Integer">
 8          <generator class="native"/>
 9       </id>
10      <property name="age" column="customer_age" type="java.lang.Integer" />
11      <property name="name" column="customer_name" type="java.lang.String" length="25" />
12            
13      <set name="orders" inverse="true" cascade="all">
14        <key column="customer_order_id" />
15        <one-to-many class="com.laoyangx.chapter0.Order"/>
16        <filter name="creationVisibleFilter" condition="order_visible=:asOfVisible" />
17      </set>
18       <filter name="creationAgeFilter" condition="customer_age>:asOfAge" />  
19   </class>
20   
21    <filter-def name="creationAgeFilter">
22      <filter-param name="asOfAge" type="java.lang.Integer"/>
23    </filter-def>
24    
25   <filter-def name="creationVisibleFilter">
26     <filter-param name="asOfVisible" type="java.lang.Boolean"/>
27   </filter-def>
28   
29 </hibernate-mapping>
复制代码

chapter0_order.hbm.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6   <class name="com.laoyangx.chapter0.Order" table="T_chapter0_orders" >
 7       <id name="id" column="order_id" type="java.lang.Integer">
 8          <generator class="native"/>
 9       </id>
10      <property name="date" column="order_date" type="java.lang.String" length="10" />
11      <property name="visible" column="order_visible" type="java.lang.Boolean" />
12      <many-to-one name="customer" class="com.laoyangx.chapter0.Customer" column="customer_order_id"></many-to-one>    
13   </class>
14 </hibernate-mapping>
复制代码

5。测试

TestFilterDef.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.laoyangx.chapter0;
 
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
 
import junit.framework.TestCase;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
public class TestFilterDef extends TestCase {
     
     /**
      * 添加顾客和订单号,用于测试
      */
     public void fillData(){
         
         Session session=HibernateUtil.currentSession();
         Transaction transaction= null ;
         
         try {
             transaction=session.beginTransaction();
             
             for ( int i= 20 ;i< 30 ;i++){
                 
                 Customer customer= new Customer(i, "customer" +i);
                 
                 boolean visible=( 0 ==i% 2 )? false : true ; //当顾客的年龄为偶数是 visible=false 反之为 true
                 Order order= new Order( new Date(),customer,visible);
                 
                 customer.getOrders().add(order);
                 
                 session.save(customer);
             }
             
             transaction.commit();
         } catch (HibernateException e) {
             
             if (transaction!= null ) transaction.rollback();
             
             e.printStackTrace();
         }
         finally {
             session.close();
         }      
     }
     
     
     /**
      * 通过指定顾客的年龄作为查询条件,获取顾客
      */
     public void getCustomerWithAge(){
         
         Session session=HibernateUtil.currentSession();
         Transaction transaciton= null ;
         
         try {
             
             transaciton=session.beginTransaction();
             session.enableFilter( "creationVisibleFilter" ).setParameter( "asOfVisible" , true );
             session.enableFilter( "creationAgeFilter" ).setParameter( "asOfAge" , new Integer( 25 ));
 
             List resultSet=(List)session.createQuery( "from Customer" ).list();
             
             Boolean resultNotEmpty=(resultSet!= null )&&resultSet.size()> 0 ;
             if (resultNotEmpty){
                 
                 for (Iterator iter=resultSet.iterator();iter.hasNext();) {
                     
                     Customer customer=(Customer)iter.next();               
                     Set<Order> orders=customer.getOrders();
                     
                     Boolean orderIsNullOrEmpty=(orders!= null )&&orders.size()> 0 ;
                     if (orderIsNullOrEmpty){
                         
                         System.out.println( "customer_id-> " +customer.getId()+ " customer_name-> " +customer.getName()
                                 + " customer_age-> " +customer.getAge());
                         
                         for (Iterator<Order> i=orders.iterator();i.hasNext();){
                             Order order=(Order)i.next();
                             System.out.println( "order_id-> " +order.getId()+ " order_date-> " +order.getDate());
                         }
                     }
                     System.out.println( "-----------------------------" );
                 }
             }
                                 
         } catch (HibernateException e) {
            
             if (transaciton!= null ) transaciton.rollback();
                 
             e.printStackTrace();
         }
         finally {
             session.close();
         }      
     }  
}

先运行 fillData()方法 ,向数据库中添加测试数据 运行之后 t_chapter0_customers和t_chapter0_orders这两张表中的数据如下:

order_visible 生成的规律是:顾客的年龄为偶数时候 order_visible=false 反之为true

运行 getCustomerWithAge() 方法 查询条件为顾客中年龄大于25且 order_visible为true(即顾客的年龄为奇数)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。【项目资源
大学生在线租房平台管理系统按照操作主体分为管理员和用户。管理员的功能包括报修管理、报修评价管理、字典管理、房东管理、房屋管理、房屋收藏管理、房屋留言管理、房屋租赁管理、租房论坛管理、公告信息管理、留言板管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生在线租房平台管理系统可以提高大学生在线租房平台信息管理问题的解决效率,优化大学生在线租房平台信息处理流程,保证大学生在线租房平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理公告,管理大学生在线租房平台信息,包括房屋管理,培训管理,报修管理,薪资管理等,可以管理公告。 房屋管理界面,管理员在房屋管理界面中可以对界面中显示,可以对房屋信息的房屋状态进行查看,可以添加新的房屋信息等。报修管理界面,管理员在报修管理界面中查看报修种类信息,报修描述信息,新增报修信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
基于hal库的OLED显示屏驱动C语言实现源码.zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip基于hal库的OLED显示屏驱动C语言实现源码.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值