Hibernate进阶学习4

深入学习hibernate的查询语句

测试HQL查询

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.List; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 测试HQL语句(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
*/
public class HibernateTest {
/**
* 排序查询
*/
@Test
public void test1() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer order by cust_id";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 统计查询
*/
@Test
public void test2() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "select count(*) from Customer";
String hql1 = "select sum(cust_id) from Customer";
String hql2 = "select avg(cust_id) from Customer";
String hql3 = "select max(cust_id) from Customer";
String hql4 = "select min(cust_id) from Customer";
//2)创建查询对象
Query query = session.createQuery(hql2);
//3)执行查询
Number number = (Number) query.uniqueResult(); System.out.println(number); /*******************************************************/ tx.commit();
session.close();
} /**
* 投影查询
*/
@Test
public void test3() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "select new Customer(cust_id,cust_name) from Customer";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
}
}
package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.Arrays;
import java.util.List; /**
* @author: XDZY
* @date: 2018/11/19 14:35
* @description: 测试HQL多表查询(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
*/
public class HibernateTest2 {
/**
* 内连接
*/
@Test
public void test1() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer c inner join c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Object[]> list = query.list(); for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
} /*******************************************************/ tx.commit();
session.close();
} /**
* 迫切内连接
* 将查询到的关联的对象也封装到查询的对象中
*/
@Test
public void test2() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer c inner join fetch c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 左(右)外连接
*/
@Test
public void test3() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
//String hql="from Customer c left join c.linkMens";
String hql = "from Customer c right join c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Object[]> list = query.list(); for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
} /*******************************************************/ tx.commit();
session.close();
}
}

测试Criteria查询

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.*;
import org.junit.Test; import java.util.List; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 测试criteria语句(hibernate独有的无语句的全面向对象的查询语法)(适合单表查询)
*/
public class HibernateTest3 {
/**
* 排序查询
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ Criteria criteria = session.createCriteria(Customer.class);
//criteria.addOrder(Order.asc("cust_id"));
criteria.addOrder(Order.desc("cust_id"));
List list = criteria.list();
System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 离线查询
* 就是在不创建session的情况下也能进行数据库操作(比如在service和web层调用)
*/
@Test
public void test1() {
//创建离线对象
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
dc.add(Restrictions.idEq(3L)); //创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ Criteria criteria = dc.getExecutableCriteria(session);
List list = criteria.list();
System.out.println(list); /*******************************************************/ tx.commit();
session.close();
}
}

测试类级别加载策略

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
<class name="Customer" table="cst_customer" lazy="false">
<id name="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property> <!-- lazy属性:决定是否延迟加载
true(默认值):延迟加载,懒加载
false:立即加载
extra:极其懒惰
fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
select(默认值):单表查询加载
join:使用多表查询加载集合
subselect:使用子查询加载集合 -->
<!-- batch-size:抓取集合的数量为3
抓取客户的集合时,一次抓取几个客户的联系人集合 -->
<set name="linkMens" batch-size="3">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_gender"></property>
<property name="lkm_name"></property>
<property name="lkm_phone"></property>
<property name="lkm_email"></property>
<property name="lkm_qq"></property>
<property name="lkm_mobile"></property>
<property name="lkm_memo"></property>
<property name="lkm_position"></property> <!-- fetch属性:决定加载的sql语句
select:使用单表查询
join:多表查询
lazy属性:决定加载时机
false:立即加载
proxy:由customer的类级别加载策略决定 -->
<many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="join" lazy="proxy">
</many-to-one>
</class>
</hibernate-mapping>
package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.*;
import org.junit.Test; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 类级别加载策略
*/
public class HibernateTest4 {
/**
* 懒加载|延迟加载
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //立即加载
//Customer customer = session.get(Customer.class, "2L"); //延迟加载:查询时只返回一个代理对象,在使用时,根据关联的session查询数据库返回结果
//为了更好的性能,建议延迟加载;延迟加载貌似只是推迟了查询
Customer customer = session.load(Customer.class, "2L");
System.out.println(customer); /*******************************************************/ tx.commit();
session.close();
}
}

测试关联级别加载策略

package com.hibernate.test;

import com.hibernate.domain.Customer;
import com.hibernate.domain.LinkMan;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.Set; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 关联级别加载策略
*/
public class HibernateTest5 {
/**
* lazy与fetch的使用
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //立即加载
Customer customer = session.get(Customer.class, "2L"); Set<LinkMan> linkMens = customer.getLinkMens();
System.out.println(linkMens.size());
System.out.println(linkMens); /*******************************************************/ tx.commit();
session.close();
}
}

Hibernate进阶学习4的更多相关文章

  1. Hibernate进阶学习3

    Hibernate进阶学习3 测试hibernate的多表关联操作(一对多,多对一,多对多) 表之间的关系主要在类与元数据配置文件中体现 package com.hibernate.domain; i ...

  2. 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)

    [SSH进阶之路]Struts + Spring + Hibernate 进阶开端(一) 标签: hibernatespringstrutsssh开源框架 2014-08-29 07:56 9229人 ...

  3. Hibernate基础学习2

    Hibernate基础学习2 测试hibernate的一级缓存,事务以及查询语句 1)Hibernate的一些相关概念 hibernate的一级缓存 1)缓存是为了提高该框架对数据库的查询速度 2)一 ...

  4. PHP程序员进阶学习书籍参考指南

    PHP程序员进阶学习书籍参考指南 @heiyeluren lastmodify: 2016/2/18     [初阶](基础知识及入门)   01. <PHP与MySQL程序设计(第4版)> ...

  5. Matlab 进阶学习记录

    最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal  =  proposal_config('image_means', ...

  6. (Hibernate进阶)Hibernate系列——总结篇(九)

    这篇博文是hibernate系列的最后一篇,既然是最后一篇,我们就应该进行一下从头到尾,整体上的总结,将这个系列的内容融会贯通. 概念 Hibernate是一个对象关系映射框架,当然从分层的角度看,我 ...

  7. zuul进阶学习(二)

    1. zuul进阶学习(二) 1.1. zuul对接apollo 1.1.1. Netflix Archaius 1.1.2. 定期拉 1.2. zuul生产管理实践 1.2.1. zuul网关参考部 ...

  8. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改

    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...

  9. Struts2进阶学习4

    Struts2进阶学习4 自定义拦截器的使用 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <! ...

随机推荐

  1. 关于老教授之家项目的思考 && 中国互联网+大赛培训

    最近在做中国互联网+竞赛相关的项目,有一点思考在这里记录下来,算是一份经历,日后可以再回顾,这也是我真正参加的一个大型比赛,作为技术人员可能更多的是从事技术,但是在其他方面能贡献自己的一份力量也是不错 ...

  2. vue学习中遇到的onchange、push、splice、forEach方法使用

    最近在做vue的练习,发现有些js中的基础知识掌握的不牢,记录一下: 1.onchange事件:是在域的内容改变时发生,单选框与复选框改变后触发的事件. 2.push方法:向数组的末尾添加一个或多个元 ...

  3. 四、Oracle loop循环、while循环、for循环、if选择和case选择、更改读取数据、游标、触发器、存储过程

    数据库的设计(DataBase Design): 针对于用户特定的需求,然后我们创建出来一个最使用而且性能高的数据库! 数据库设计的步骤: 01.需求分析 02.概念结构设计 03.逻辑结构设计 04 ...

  4. 前台异步传过来的URL中获取token/获取string链接中的token

    1.链接例子: string url = "http://domainName:port/1/2/3/4.htm?sysCode=1001020&token=QXJzemR3YXlW ...

  5. 关于objc.io

    推荐一个特别棒的项目:objc.io 原版地址:http://www.objc.io/ 中国版地址:http://objccn.io/ 欢迎大家前去学习,如果你有不错的东西,也欢迎跟帖分享.

  6. node安装express时找不到pakage.json文件;判断安装成功?

    正常安装命令:express install express --save 报错如下:no such file or directory,open 'C:\Users\Administrator\pa ...

  7. (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]

    10].正则表达式 /** * 正则表达式(Regular Expression): * * 用于文本搜索和文本替换 * */ /** * /good/i是一个正则表达式. * good是一个模式(用 ...

  8. linux下查找字符串

    如果你想在当前目录下 查找"hello,world!"字符串,可以这样: grep -rn "hello,world!" * * : 表示当前目录所有文件,也可 ...

  9. open ssh 常用的东西

    清除已经存在的但是不同设备的连接信息 ssh-keygen -f "/users/he/.ssh/known_hosts" -R 192.168.1.118 无密码登录openss ...

  10. POJO详解

    转自:http://blog.csdn.net/lushuaiyin/article/details/7436318  一:什么是POJO POJO的名称有多种,pure old java objec ...