java

位置:IT落伍者 >> java >> 浏览文章

Hibernate 命名查询NamedQuery


发布日期:2022年08月31日
 
Hibernate 命名查询NamedQuery

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

房屋实体类

Housejava

package cnjbithouserentbean;

import javautilDate;

public class House implements javaioSerializable {

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 thisid;

}

public void setId(Integer id) {

thisid = id;

}

public User getUser() {

return thisuser;

}

public void setUser(User user) {

thisuser = user;

}

public Type getType() {

return thistype;

}

public void setType(Type type) {

thistype = type;

}

public Street getStreet() {

return thisstreet;

}

public void setStreet(Street street) {

thisstreet = street;

}

public String getTitle() {

return thistitle;

}

public void setTitle(String title) {

thistitle = title;

}

public String getDescription() {

return thisdescription;

}

public void setDescription(String description) {

thisdescription = description;

}

public Double getPrice() {

return thisprice;

}

public void setPrice(Double price) {

thisprice = price;

}

public Date getDate() {

return thisdate;

}

public void setDate(Date date) {

thisdate = date;

}

public Integer getFloorage() {

return thisfloorage;

}

public void setFloorage(Integer floorage) {

thisfloorage = floorage;

}

public String getContact() {

return ntact;

}

public void setContact(String contact) {

ntact = contact;

}

}

配置映射文件

Househbmxml

<?xml version= encoding=utf?>

<!DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN

hiber/hibernatemappingdtd>

<!

Mapping file autogenerated by MyEclipse Persistence Tools

>

<hibernatemapping>

<class name=cnjbithouserentbeanHouse table=house lazy=false schema=jbit>

<id name=id type=javalangInteger>

<column name=id />

<generator class=sequence >

<param name=sequence>SEQ_ID</param>

</generator>

</id>

<manytoone name=user class=cnjbithouserentbeanUser cascade=none fetch=join>

<column name=user_id />

</manytoone>

<manytoone name=type class=cnjbithouserentbeanType cascade=none fetch=join>

<column name=type_id />

</manytoone>

<manytoone name=street class=cnjbithouserentbeanStreet cascade=none fetch=join>

<column name=street_id />

</manytoone>

<property name=title type=javalangString>

<column name=title length= notnull=false />

</property>

<property name=description type=text lazy=false>

<column name=description />

</property>

<property name=price type=javalangDouble>

<column name=price precision= scale= notnull=false />

</property>

<property name=date type=javautilDate>

<column name=pubdate length= />

</property>

<property name=floorage type=javalangInteger>

<column name=floorage length= />

</property>

<property name=contact type=javalangString>

<column name=contact length= />

</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>

</hibernatemapping>

封装查询参数

QueryPropertiesjava

package cnjbithouserentutil;

import javautilDate;

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

thishigh_price = high_price;

}

public Double getLow_price() {

return low_price;

}

public void setLow_price(Double low_price) {

thislow_price = low_price;

}

public String getType_id() {

return type_id;

}

public void setType_id(String type_id) {

thistype_id = type_id;

}

public String getStreet_id() {

return street_id;

}

public void setStreet_id(String street_id) {

thisstreet_id = street_id;

}

public Date getStart_date() {

return start_date;

}

public void setStart_date(Date start_date) {

thisstart_date = start_date;

}

public Date getEnd_date() {

return end_date;

}

public void setEnd_date(Date end_date) {

thisend_date = end_date;

}

public Integer getSmall_floorage() {

return small_floorage;

}

public void setSmall_floorage(Integer small_floorage) {

thissmall_floorage = small_floorage;

}

public Integer getBig_floorage() {

return big_floorage;

}

public void setBig_floorage(Integer big_floorage) {

thisbig_floorage = big_floorage;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

thistitle = title;

}

}

测试代码

Testjava

import javautilIterator;

import javautilList;

import orghibernateHibernateException;

import orghibernateQuery;

import orghibernateSession;

import orghibernateSessionFactory;

import orghibernateTransaction;

import cnjbithibernateentityHouse;

import cnjbithibernateentityQueryProperties;

import cnjbithibernateentityUser;

import cnjbithibernateutilHibernateUtil;

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=ugetSession();

tx=sessionbeginTransaction();

Query query =sessiongetNamedQuery(queryHouse);//获取命名查询语句

//给封装参数类添加参数值

QueryProperties qp = new QueryProperties();

qpsetTitle(豪放);

qpsetLow_price(new Integer());

qpsetHigh_price(new Integer());

qpsetStreet_id();

qpsetType_id();

qpsetSmall_floorage(new Integer());

qpsetBig_floorage(new Integer());

querysetProperties(qp);

List result =querylist();

Iterator it = erator();

if(ithasNext()){

House house=(House)itnext();

Systemoutprintln(housegetTitle()+ +housegetPrice());

}*/

//String hql = from User u left join fetch uhouse h ;

String hql=from District d inner join dstreet s;

Query query = sessioncreateQuery(hql);

List list=querylist();

}catch(HibernateException e){

eprintStackTrace();

}

}

}

注意这里没有写出配置文件和创建工厂会话等代码

               

上一篇:分析Hibernate的事务处理机制

下一篇:Eclipse编辑spring配置文件时提示功能