Hibernate HQL查询

来源:互联网 发布:2016广告投放数据分析 编辑:程序博客网 时间:2024/06/07 06:57
HQL 大小写不敏感,当有相同的实体类名时,使用包名.实体类
Query query=session.createQuery("from Cat c");

List<Cat> catlist=query.list();


返回单个对象
Query q=session.createQuery("select count(c) from Cat c");
Number num=(Number)q.uniqueResult(); //返回单个实例
int count=num.intValue(); //返回数值
查询总数时,HQL 格式必须为以上语句格式,返回值可能为

Short Integer Long BigInteger 具体由主键的类型而定


返回 Object[]数组
List<Object[]> list=session.createQuery
("select c.name,c.mother from Cat c").list();
//获取
for(Object[] row : list){
for(Object obj : row){
System.out.println(obj);
}

}


返回 List 类型
String hql="select new List(c.name,c.mother) from Cat c"'
List<List> list=session.cresteQuery(hql).list();
//获取
For(List row : list){
For(Object obj : row){
System.out.println(obj);
}

}


返回 Map 类型
String hql="sesect new map(c.name as name,c.mother as mother)" + "from Cat c" ;
List listMap=session.creatgQuery(hql).list();
//获取
for(Map map : (List<Map> listMap){
System.out.println("Name:"+map.get("name"));
System.out.println("Mother:"+map.get("mother"));

}


返回实体对象
String hql="select new Cat(cat.name,cat.createDate) from Cat c ";

List<Cat> catList=session.createQuery(hql).list();


注意:
这样使用时,Cat 类中必须存在一个 public Cat(String name,Date createDate)
的构造方法。因为 Hibernate 是通过调用该构造方法完成返回值从 Object[]数组
转化到 Cat 类实体类的。
条件查询和运算符
Where 子句的语法
String hql="select c from Cat c"+" where c.mother.name=null
and c.createDate<:createDate";
session.createQuery(hql).setParameter("createDate",new Date()).list();

表达式 setParameter()传递参数


HQL 支持的运算符
� 数学运算符:+ - * /
� 比较操作符:= != < <= > >= like
� 逻辑计算法:and or not
� SQL 操作符:in 、not in 、between 、is null 、is not null
is empty 、number of
� 字符串连接:|| 或 concat("","")
� 时间日期函数:current_date() current_time() current_timestamp()
Second() minute() hour() day() month() year()
� JPA 定义的操作:substring() coalesce() lower() upper() lenth()
Locate() abs() sqrt() bit_lenth() nullif() trim()
� 数据库支持的 SQL 标量函数:sign() trunc() rtrim() sin()

� 简单的跳转语句:case...when...then...else...end


语法:看高级查询,设置查询条件时,应尽量使用 setParameter()传递参数。


统计函数
String hql="select count(c) from Cat c where c.mother !=null";
Number num=(Number)session.createQuery(hql).uniqueResult();

int count=num.intValue(); //同返回单个对象


HQL 分页显示
String hql="select count(c) from Cat c";
Long count=(Long)session.createQuery(hql).uniqueResult(); //查询记录总数
List<Cat> ccList=session.createQuery("from Cat")
.setFirstResult(0) //从第 0 条开始

.setMaxResults(10).list(); //取 10 条数据


HQL 连表查询
String hql=" select e from Event e where e.cat.name='Ketty' ";
List<Event> eventList=sessionQuery(hql).list();
Where 子句用到了 Car 表
String hql="select c from Cat c left join c.events e
where e.description like :str";

List<Cat> list=session.createQuery(hql).setParameter("str","%dd%").list();


使用 SQL 数据库
Hql 为 jdbc 格式
String hql="select * from tb_cat";
SQLQuery sqlquery=session.createSQLQuery(hql);
sqlquery.addEntity(Cat.class); //设置输出类型
List<Cat> catList=sqlquery.list();
命名常用的查询
实体类中@注解配置,使用直接引用名字。
@NamedQuery(name="cat",query="select c from Cat c")
@NamedNativeQuery(name="cat",query="select * from tb_cat)
0 0