知识大全 Hibernate 命名查询NamedQuery
Posted 语句
篇首语:少年意气强不羁,虎胁插翼白日飞。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Hibernate 命名查询NamedQuery相关的知识,希望对你有一定的参考价值。
Hibernate 命名查询NamedQuery 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
例子 使用命名查询实现多条件对租房信息进行模糊查询
房屋实体类
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 ntact;
public void setContact(String contact)
ntact = contact;
配置映射文件
House hbm xml
<?xml version= encoding= utf ?>
<!DOCTYPE hibernate mapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
hiber/hibernate mapping 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= 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= scale= not null= false />
</property>
<property name= date type= java util Date >
<column name= pubdate length= />
</property>
<property name= floorage type= java lang Integer >
<column name= floorage length= />
</property>
<property name= contact type= java lang String >
<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 beeen :low_price and :high_price) and
(street_id like :street_id) and (type_id like :type_id) and
(floorage beeen :small_floorage and :big_floorage)
]]>
</query>
</hibernate mapping>
封装查询参数
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;
测试代码
Test java
import java util Iterator;
import java util List;
import hibernate HibernateException;
import hibernate Query;
import hibernate Session;
import hibernate SessionFactory;
import 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( ));
qp setHigh_price(new Integer( ));
qp setStreet_id( );
qp setType_id( );
qp setSmall_floorage(new Integer( ));
qp setBig_floorage(new Integer( ));
query setProperties(qp);
List result =query list();
Iterator it = erator();
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();
cha138/Article/program/Java/ky/201311/28724
相关参考
在hibernate中是支持多条件动态查询的这种问题是非常实用的比如说你做一个项目要实现一个复合查询 例如有这么一个界面 那么要实现这个查询对于用户来说他的查询条件可能不健全有可能只填写其
Hibernate本地SQL查询 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 本地SQL查询来
Hibernate复合主键查询 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Hibernate
Hibernate数据查询 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! HibernateQu
Hibernate高级查询技巧 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 集合过滤: 对于
Hibernate高级查询实战 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 大家知道在Hibe
Hibernate属性查询简介 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 本文向大家介绍Hi
首先定义一个条件查询实例这里要有一个主表(mainTable) DetachedCriteriadeCriteria=DetachedCriteriaforClass(mainTableclas
Hibernate查询方法之探析 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! :QBE(Que
Hibernate高级查询实战[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 《取TOP结