Hibernate HQL查询
来源:互联网 发布:2016广告投放数据分析 编辑:程序博客网 时间:2024/06/07 06:57
HQL 大小写不敏感,当有相同的实体类名时,使用包名.实体类
Query query=session.createQuery("from Cat c");
Query q=session.createQuery("select count(c) from Cat c");
Number num=(Number)q.uniqueResult(); //返回单个实例
int count=num.intValue(); //返回数值
查询总数时,HQL 格式必须为以上语句格式,返回值可能为
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);
}
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);
}
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 ";
这样使用时,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();
� 数学运算符:+ - * /
� 比较操作符:= != < <= > >= 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()
String hql="select count(c) from Cat c where c.mother !=null";
Number num=(Number)session.createQuery(hql).uniqueResult();
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 条开始
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";
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)
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 具体由主键的类型而定
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);
}
}
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);
}
}
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()传递参数
� 数学运算符:+ - * /
� 比较操作符:= != < <= > >= 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(); //同返回单个对象
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 条数据
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();
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
- 【Hibernate】Hibernate HQL 查询
- Hibernate查询语言:HQL
- hibernate查询语句--HQL
- Hibernate 查询语言(HQL)
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- Hibernate查询语言:HQL
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- Hibernate查询语句+HQL
- Hibernate查询语言:HQL
- 查询 Hibernate HQL
- hibernate查询语句---HQL
- Hibernate查询语言HQL
- 【图】O(VE)的匈牙利(C)
- 关于JOptionPane.showConfirmDialog()确认框的操作
- CListBox
- 实现文字垂直居中
- java学习之路之播放一首简单的音乐
- Hibernate HQL查询
- PAT1020. 月饼 (25)
- 会议
- Spring事务配置的五种方式
- 单例模式
- dede:channelArtlist 排序
- android用NFS的形式挂载工作.
- test
- Oracle获取Oracle rowid信息的存储过程