例子: 使用命名查询实现多条件对租房信息进行模糊查询
1.房屋实体类
House.java
package cn.jbit.houserent.bean;
import java.util.Date;
public class House implements java.io.Serializable {
private Integer id;
private User user;
private Type type;
private Street street;
private String title;
private String description;
private Double price;
private Date date;
private Integer floorage;
private String contact;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public Type getType() {
return this.type;
}
public void setType(Type type) {
this.type = type;
}
public Street getStreet() {
return this.street;
}
public void setStreet(Street street) {
this.street = street;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getFloorage() {
return this.floorage;
}
public void setFloorage(Integer floorage) {
this.floorage = floorage;
}
public String getContact() {
return this.contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
2.配置映射文件
House.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.jbit.houserent.bean.House" table="house" lazy="false" schema="jbit">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence" >
<param name="sequence">SEQ_ID</param>
</generator>
</id>
<many-to-one name="user" class="cn.jbit.houserent.bean.User" cascade="none" fetch="join">
<column name="user_id" />
</many-to-one>
<many-to-one name="type" class="cn.jbit.houserent.bean.Type" cascade="none" fetch="join">
<column name="type_id" />
</many-to-one>
<many-to-one name="street" class="cn.jbit.houserent.bean.Street" cascade="none" fetch="join">
<column name="street_id" />
</many-to-one>
<property name="title" type="java.lang.String">
<column name="title" length="50" not-null="false" />
</property>
<property name="description" type="text" lazy="false">
<column name="description" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="10" scale="4" not-null="false" />
</property>
<property name="date" type="java.util.Date">
<column name="pubdate" length="23" />
</property>
<property name="floorage" type="java.lang.Integer">
<column name="floorage" length="10" />
</property>
<property name="contact" type="java.lang.String">
<column name="contact" length="10" />
</property>
</class>
<---在映射文件中.<query>元素用于定义一个HQL查询语句,它和<class>元素并列。给HQL语句命名"queryHouse",以<![CDATA[ HQL ]]>方式保存HQL语句,在程序中通过Session对象的getNamedQuery()方法获取该查询语句 -->
<query name="queryHouse">
<![CDATA[
from House where (title like :title) and
(price between :low_price and :high_price) and
(street_id like :street_id) and (type_id like :type_id) and
(floorage between :small_floorage and :big_floorage)
]]>
</query>
</hibernate-mapping>
3。封装查询参数
QueryProperties.java
package cn.jbit.houserent.util;
import java.util.Date;
public class QueryProperties {
private String title;
private Double high_price;
private Double low_price;
private Date start_date;
private Date end_date;
private String type_id;
private String street_id;
private Integer small_floorage;
private Integer big_floorage;
public Double getHigh_price() {
return high_price;
}
public void setHigh_price(Double high_price) {
this.high_price = high_price;
}
public Double getLow_price() {
return low_price;
}
public void setLow_price(Double low_price) {
this.low_price = low_price;
}
public String getType_id() {
return type_id;
}
public void setType_id(String type_id) {
this.type_id = type_id;
}
public String getStreet_id() {
return street_id;
}
public void setStreet_id(String street_id) {
this.street_id = street_id;
}
public Date getStart_date() {
return start_date;
}
public void setStart_date(Date start_date) {
this.start_date = start_date;
}
public Date getEnd_date() {
return end_date;
}
public void setEnd_date(Date end_date) {
this.end_date = end_date;
}
public Integer getSmall_floorage() {
return small_floorage;
}
public void setSmall_floorage(Integer small_floorage) {
this.small_floorage = small_floorage;
}
public Integer getBig_floorage() {
return big_floorage;
}
public void setBig_floorage(Integer big_floorage) {
this.big_floorage = big_floorage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
4测试代码
Test.java
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import cn.jbit.hibernate.entity.House;
import cn.jbit.hibernate.entity.QueryProperties;
import cn.jbit.hibernate.entity.User;
import cn.jbit.hibernate.util.HibernateUtil;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
HibernateUtil u= new HibernateUtil();
SessionFactory sf = null;
Session session =null;
Transaction tx=null;
try{
session=u.getSession();
tx=session.beginTransaction();
Query query =session.getNamedQuery("queryHouse");//获取命名查询语句
//给封装参数类添加参数值
QueryProperties qp = new QueryProperties();
qp.setTitle("豪放");
qp.setLow_price(new Integer(10000));
qp.setHigh_price(new Integer(25000));
qp.setStreet_id(1001);
qp.setType_id(1001);
qp.setSmall_floorage(new Integer(90));
qp.setBig_floorage(new Integer(180));
query.setProperties(qp);
List result =query.list();
Iterator it = result.iterator();
if(it.hasNext()){
House house=(House)it.next();
System.out.println(house.getTitle()+" "+house.getPrice());
}*/
//String hql = "from User u left join fetch u.house h ";
String hql="from District d inner join d.street s";
Query query = session.createQuery(hql);
List list=query.list();
}catch(HibernateException e){
e.printStackTrace();
}
}
}
注意:这里没有写出配置文件和创建工厂会话等代码