Hibernate的过滤器类似Servlet的过滤器,对获取的数据进行过滤处理。
Hibernate的过滤器的过滤条件,需要配置在映射文件中。
通过调用Session对象的setFilter()和enableFilter()方法使用过滤器。
举个例子:比如我们通过Hibernate查询获取了1000条记录,那么我们可以通过设置好的过滤器,安装某些条件
进行过滤。最终获得满足这些条件的小于1000条的记录,如经过过滤我们得到了300条记录。这就是Hibernate过滤器的作用。
PS:当然我们也可以通过使用HQL语句实现我们需要的查询结果,但是使用Filter要方便一点,容易修改
现在我们来实际操作一下:
新建java project项目:chapter15_filter
Add Hibernate Capabilities
数据库表:
user:id ,username,password
profile:id,email,phone,mobil,address,postcode,user_id
POJO类
User.java 和 Profile.java
User.java
代码:
package com.b510.examples;
public class User implements java.io.Serializable { private static final long serialVersionUID = -580528678939921966L; private Integer id; private String username; private String password; private Profile profile;
public Profile getProfile() { return profile; }
public void setProfile(Profile profile) { this.profile = profile; }
public User() { }
public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
public String getUsername() { return this.username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return this.password; }
public void setPassword(String password) { this.password = password; }
}
Profile.java
代码:
package com.b510.examples;
public class Profile implements java.io.Serializable { private static final long serialVersionUID = -3961555762499504617L; private Integer id; private String email; private String phone; private String mobile; private String address; private String postcode; private User user;
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public Profile() { }
public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return this.phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getMobile() { return this.mobile; }
public void setMobile(String mobile) { this.mobile = mobile; }
public String getAddress() { return this.address; }
public void setAddress(String address) { this.address = address; }
public String getPostcode() { return this.postcode; }
public void setPostcode(String postcode) { this.postcode = postcode; }
}
User.hbm.xml
代码:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.b510.examples.User" table="user" catalog="users"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="200" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="200" /> </property> <one-to-one name="profile" class="com.b510.examples.Profile"></one-to-one>
<!-- 使用过滤器 名字:nameFilter --> <filter name="nameFilter" condition="USERNAME=:inputName"></filter> </class>
<!-- 定义过滤器 名字:nameFilter --> <filter-def name="nameFilter" > <filter-param name="inputName" type="java.lang.String"/> </filter-def> </hibernate-mapping>
Profile.hbm.xml
代码:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.b510.examples.Profile" table="profile" catalog="users"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="foreign"> <param name="property">user</param> </generator> </id> <property name="email" type="java.lang.String"> <column name="email" length="200" /> </property> <property name="phone" type="java.lang.String"> <column name="phone" length="20" /> </property> <property name="mobile" type="java.lang.String"> <column name="mobile" length="20" /> </property> <property name="address" type="java.lang.String"> <column name="address" length="200" /> </property> <property name="postcode" type="java.lang.String"> <column name="postcode" length="20" /> </property> <one-to-one name="user" class="com.b510.examples.User"></one-to-one> </class> </hibernate-mapping>
测试代码:
HIbernateTest.java
代码:
/** * */ package com.b510.examples;
import java.util.List;
import org.hibernate.Filter; import org.hibernate.Query; import org.hibernate.Session;
/** * * @author XHW * * @date 2011-6-9 * */ public class HibernateTest {
public static void main(String[] args) { new HibernateTest().useFilter();
} public void useFilter() { Session session = HibernateSessionFactoryUtil.getSessionFactory() .getCurrentSession(); session.beginTransaction(); String hql="from User where id between 1 and 50"; Query query=session.createQuery(hql); Filter filter=session.enableFilter("nameFilter"); filter.setParameter("inputName", "hanyuan1"); List<User> list=query.list(); session.getTransaction().commit(); for(User user:list){ System.out.println("id: "+user.getId()+" username: "+user.getUsername()); } } }
运行效果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate: select user0_.id as id1_, user0_.username as username1_, user0_.password as password1_ from users.user user0_ where user0_.USERNAME=? and ( user0_.id between 1 and 50 ) Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? Hibernate: select profile0_.id as id0_2_, profile0_.email as email0_2_, profile0_.phone as phone0_2_, profile0_.mobile as mobile0_2_, profile0_.address as address0_2_, profile0_.postcode as postcode0_2_, user1_.id as id1_0_, user1_.username as username1_0_, user1_.password as password1_0_, profile2_.id as id0_1_, profile2_.email as email0_1_, profile2_.phone as phone0_1_, profile2_.mobile as mobile0_1_, profile2_.address as address0_1_, profile2_.postcode as postcode0_1_ from users.profile profile0_ left outer join users.user user1_ on profile0_.id=user1_.id left outer join users.profile profile2_ on user1_.id=profile2_.id where profile0_.id=? id: 6 username: Hanyuan1 id: 7 username: Hanyuan1 id: 8 username: Hanyuan1 id: 9 username: Hanyuan1 id: 10 username: Hanyuan1 id: 11 username: Hanyuan1 id: 12 username: Hanyuan1