hql语句的查询(hibernate query language)

hql和sql语句的区别
sql:语言关系型数据库里面的通用查询
,结构化查询语言,查看的是表以及表的列
hql是hibernate中独有的查询语言,其是面向
对象的查询语句,查询的是类以及类中的属性
注意:hql语句不能执行insert语句,且最终
编写的hql语句会转化为相应的sql语句
,以及hibernate的get/load方法只能通过主键的方式
进行查询
from User u where u.name='zhangsan' and u.age>20
update User u set u.name='briup' where u.id=10;
delete from User u where u.name='briup'
session.delete

Criteria
作用:指定查询某一个类所对应表里面的数据
对象的获得:session可以创建
Criterion
作用:表示查询时用到的条件,每个条件都是一个
Criterion类型的对象 eg:id>5、salary=1000
对象的获取:使用Restrictions创建
Restrictions
作用:专门创建表示查询条件的对象,
也就是Criterion类型的对象
使用的是这个类里面的静态方法来创建表示
条件的对象

高级查询方式
Criteria(接口)
1.概念
Criterion(接口)是criteria的查询条件。
criteria提供了
add(Criterion criterion)方法来添加查询条件

2、创建
Criterion的实例可以通过Restrictions工具类来
创建,Restrictions提供了大量的静态的方法,如
eq(等于)、ge(大于等于)、between 等方法的创建
Criterion查询条件

Restrictions.eq =
Restrictions.gt >
Restrictions.ge >=
Restrictions.lt <
Restrictions.le <=
Restrictions.between BETWEEN
Restrictions.like like
Restrictions.in in
Restrictions.and and
Restrictions.or or
Restrictions.isNull 判断是否为空,为空则返回true
Restrictions.isNotNull 与isNull相反

测试实例:

实体类一:

package com.briup.criteria.bean;
public class Husband {
private long id;
private String name;
private int age;
private double salary;
private Wife wife;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}

实体类二:

package com.briup.criteria.bean;
public class Wife {
private long id;
private String name;
private Husband husband;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}

配置文件一:

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.briup.criteria.bean.Husband" table="s_husband">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="age"/>
<property name="salary"/>
<many-to-one name="wife" class="com.briup.criteria.bean.Wife" column="wife_id" unique="true" cascade="all" ></many-to-one>
</class>
</hibernate-mapping>

配置文件二:

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.briup.criteria.bean.Wife" table="s_wife">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<one-to-one name="husband" class="com.briup.criteria.bean.Husband" cascade="all" fetch="select"></one-to-one>
</class>
</hibernate-mapping>

测试类:

package com.briup.criteria.test;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.briup.criteria.bean.Husband;
import com.briup.criteria.bean.Wife;
/**
* 高级查询方式Criteria(接口)测试实例
* 1.普通查询;
* 2.添加条件查询[where\and\or\order by\between and\ > < = null\not null\in\like
* 3.多对象查询和排序
* 4.hql语句分页
* @author Victor
*
*/
public class Criteria_Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
private Configuration configuration=new Configuration();
@Before
public void before(){
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
session=sessionFactory.openSession();
transaction=session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
}
@Test
public void AutoCreateTable(){
SchemaExport schemaExport=new SchemaExport(configuration);
schemaExport.create(true, true);
}
@Test
public void save(){
for(int i=0;i<=8;i++){
Husband hus=new Husband();
hus.setName("briup"+i);
hus.setAge(20+i);
hus.setSalary(2000+(i*300));
Wife wife=new Wife();
wife.setName("lili"+i);
hus.setWife(wife);
wife.setHusband(hus);
session.save(wife);
}
}
@Test
public void singleSave(){
Husband husband=new Husband();
husband.setName("奎因");
husband.setAge(35);
husband.setSalary(3500);
Wife wife=new Wife();
wife.setName("蛮子");
husband.setWife(wife);
wife.setHusband(husband);
//session.save(husband);
session.save(wife);
}
@Test//查询所有的数据
public void criteriaSelectHus(){
//session的作用:
//1执行增删改查操作
//2提供缓存功能
//3创建高级查询接口的实现类
//Criteria是接口eg:select * from s_husband
//Criteria criteria=session.createCriteria(Husband.class);
//这时候就可以查询出所有的结果,在这里没有加任何限定条件
/*List<Husband> list=criteria.list();
for(Husband h:list){
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
@Test
public void criteraSelectWif(){
//Criteria criteria=session.createCriteria(Wife.class);
/*List<Wife> list=criteria.list();
for(Wife w:list){
System.out.println(w.getId()+";"+w.getName());
}*/
}
@Test//添加查询条件
public void criteriaSelect(){
Criteria criteria=session.createCriteria(Husband.class);
/***
* criteria接口 Criterion接口Restrictions类
* Criterion是criteria的查询条件-------where
* Criterion也是一个接口,我们要使用这个接口的实现类对象来表示查询条件
* Criteria中的add(查询条件)
* Criteria提供了add(Criterion criterion)方法来添加查询条件
* Restrictions工具类可以创建Criterion接口类型的对象,也就是创建查询条件对象
* Restrictions类中有很多的静态方法,这些方法的返回值就是我们要查询条件对象
*
* Restrictions.gt("id",2L)方法的返回值是Criterion接口的实现类对象
* select * from s_husband where id>2;
* criteria.add(Restrictions.gt("id",2L));
*
*
* select * from s_husband where salary<=4000;
* criteria.add(Restrictions.le("salary",4000d));
*
* select * from s_husband where name='briup1';
* criteria.add(Restrictions.eq("name","briup1"));
*
* select * from s_husband where name like 'briu%';
* criteria.add(Restrictions.like("name","briu%"));
*
* select * from s_husband where id in(1,2,3,4);
* criteria.add(Restrictions.in("id",new Long[]{1L,2L,3L,4L}));
*
* select * from s_husband where salary between 4000 and 6000;
* criteria.add(Restrictions.between("salary",4000d,6000d));
* 注意:这里同样可以出现两个日期之间的值
*
* select * from s_husband where salary is null;
* criteria.add(Restrictions.isNull("salary"));
*
*
*/
//吧类和类对应的表字段让其不一样,测试用的是表里的字段还是类里的字段
// criteria.add(Restrictions.gt("id",2L))
// .add(Restrictions.le("salary",4000d));
criteria.add(Restrictions.isNotNull("salary"));
//这时候就可以查询出所有的结果,在这里没有加任何限定条件
/*List<Husband> list=criteria.list();
for(Husband h:list){
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
@Test
public void select(){
Criteria criteria=session.createCriteria(Husband.class);
//.add(Restrictions.le("salary", 3800D))
//.add(Restrictions.gt("id",2L));;
//.add(Restrictions.le("salary",4000d));
//.add(Restrictions.eq("name","周志伟"));
//criteria.add(Restrictions.like("name","briu%"));
//criteria.add(Restrictions.isNotNull("salary"));
criteria.add(Restrictions.in("id",new Long[]{1L,2L,3L}));
//criteria.add(Restrictions.between("salary",4000d,6000d));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId()+";"+h.getName()+";"+h.getAge()+";"+h.getSalary());
}*/
}
@Test//连着添加查询语句
public void criteia3(){
Criteria criteria=session.createCriteria(Husband.class);
//连着添加条件,默认是以and方式添加链接条件
//select * from s_husband where id>3 and id<7;
criteria.add(Restrictions.gt("id",2L));
criteria.add(Restrictions.lt("id",7L));
//上面的写法等价于下面的写法
//下面的写法可以给我们带来好处,链式编程,eg:String中的replace方法。我们就可以一直替换
//criteria.add(Restrictions.gt("id", 2L))
// .add(Restrictions.lt("id", 7L));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
@Test//添加or的条件
public void cretica4(){
Criteria criteria=session.createCriteria(Husband.class);
//Restrictions.or(条件1,条件2);方法的返回值还是Criterion类型对象
//select * from s_husband where (id>2 or name='briup1')
//criteria.add(Restrictions.or(Restrictions.gt("id", 2L),Restrictions.eq("name", "briup1")));
//Restrictions.disjunction()后面可以跟多个or
//关联条件
//select * from s_husband where id>3 and id id<7 and ....or...or..or
criteria.add(Restrictions.disjunction()
.add(Restrictions.gt("salary", 2000))
.add(Restrictions.eq("name", "briup2"))
.add(Restrictions.eq("id", 4L))
).add(Restrictions.gt("age", 22));
// criteria.add(Restrictions.gt("id",3L))
// .add(Restrictions.lt("id", 7L))
// .add(Restrictions.disjunction()
// .add(Restrictions.eq("name", "briup1"))
// .add(Restrictions.like("name","bri%"))
// .add(Restrictions.lt("salary",5000D)));
/*List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
@Test//连接查询
public void creteria5(){
//fetch="join"
//Criteria criteria=session.createCriteria(Husband.class);
//在查询husband的基础上连接查询把wife也查出来
//这里的wife指的是husband中名字叫wife的属性
//criteria.createCriteria("wife");
/*List<Husband> list=criteria.list();
for(Husband h:list){
//System.out.println(h);
//测试是否是懒加载
//System.out.println(h.getWife().getName());
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary()+"--"+h.getWife().getId()+";"+h.getWife().getName());
}*/
}
@Test//排序
public void creteria6(){
//select * from s_husband
//Criteria criteria=session.createCriteria(Husband.class);
//where name="briup1";
//criteria.add(Restrictions.eq("name", "briup1"));
//order by id asc
//criteria.addOrder(Order.desc("id"));
/*criteria.addOrder(Order.desc("salary"));
List<Husband> list=criteria.list();
for(Husband h:list){
System.out.println(h.getId());
}*/
}
//hql分页操作
@Test
public void divitePage(){
String hql="from Husband";
Query query=session.createQuery(hql);
//从什么位置开始,默认的是0
int a=9;
//限定返回的条数
int b=3;
query.setFirstResult(a);
query.setMaxResults(b);
/*List<Husband> list=query.list();
for(Husband h:list){
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getAge()+"--"+h.getSalary());
}*/
}
}

Hibernate学习笔记5的更多相关文章

  1. Hibernate学习笔记(二)

    2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...

  2. Hibernate学习笔记(一)

    2016/4/18 19:58:58 Hibernate学习笔记(一) 1.Hibernate框架的概述: 就是一个持久层的ORM框架. ORM:对象关系映射.将Java中实体对象与关系型数据库中表建 ...

  3. Hibernate 学习笔记一

    Hibernate 学习笔记一 今天学习了hibernate的一点入门知识,主要是配置domain对象和表的关系映射,hibernate的一些常用的配置,以及对应的一个向数据库插入数据的小例子.期间碰 ...

  4. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

  5. Hibernate学习笔记

    一.Hibernate基础 1.Hibernate简介 Hibernate是一种对象关系映射(ORM)框架,是实现持久化存储的一种解决方案.Java包括Java类到数据库表的映射和数据查询及获取的方法 ...

  6. Hibernate学习笔记(四)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...

  7. Hibernate学习笔记(三)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...

  8. HIbernate学习笔记(一) 了解hibernate并搭建环境建立第一个hello world程序

    Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了轻量级的封装,Java程序员可以使用面向对象的编程思维来操纵数据库,它通过对象属性和数据库表字段之间的映射关系,将对象 ...

  9. Hibernate学习笔记-Hibernate关系映射

    1. 初识Hibernate——关系映射 http://blog.csdn.net/laner0515/article/details/12905711 2. Hibernate 笔记8 关系映射1( ...

  10. Hibernate学习笔记(1)Hibernate构造

    一 准备工作 首先,我们将创建一个简单的基于控制台(console-based)Hibernate应用. 我们所做的第一件事就是创建我们的开发文件夹.并把所有需要用到的Java件放进去.解压缩从Hib ...

随机推荐

  1. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ProcessBar)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...

  2. 使用JVMTI创建调试和监控代理

    Java 虚拟机工具接口(JVMTI)提供了一个编程接口,允许你(程序员)创建software agent 来监视和控制你的Java应用. JVMTI 代替了原来的Java Virtual Machi ...

  3. tyvj1106 登山

    背景     在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结……    不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴 ...

  4. 明晨HOSTS编辑器mcHostsEdtor与火狐HostAdmin配合使用

    在开发过程中,需要经常切换环境开发.测试.Stage和正式环境,甚为麻烦. 后来找到了HOST切换工具mcHostsEdtor工具快速切换host,但浏览器比如有HOST缓存,后来同事推荐FireFo ...

  5. cain使用教程

    Cain & Abel 是由Oxid.it开发的一个针对Microsoft操作系统的免费口令恢复工具.号称穷人使用的L0phtcrack.它的功能十分强大,可以网络嗅探,网络欺骗,破解加密口令 ...

  6. python3 黑板客爬虫闯关游戏(一)

    这是学习python爬虫练习很好的网站,强烈推荐! 地址http://www.heibanke.com/lesson/crawler_ex00/ 第一关猜数字 很简单,直接给出代码 import ur ...

  7. [转]hql 语法与详细解释

    HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hib ...

  8. testng 教程

    Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations  注释,如 @test ...

  9. Java GridBagLayout 简单使用

    这里只介绍了很基础布局构建及使用,主要是关于 GridBagLayout. 首先整套流程大概是, 声明一个 GridBagLayout 对象 private GridBagLayout gridBag ...

  10. Yii 动作过滤的方法

    protected function _init() { } public function beforeAction($action) { //黑名单 $blackList = array('tes ...