Hibernate 命名查询NamedQuery (一)

2014-11-24 11:10:38 · 作者: · 浏览: 0

例子: 使用命名查询实现多条件对租房信息进行模糊查询

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" >
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">






SEQ_ID






























<---在映射文件中.元素用于定义一个HQL查询语句,它和元素并列。给HQL语句命名"queryHouse",以方式保存HQL语句,在程序中通过Session对象的getNamedQuery()方法获取该查询语句 -->


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)
]]>


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_pr