OID(主键)
进行检索。get()
方法
Customer customer = session.get(Customer.class,1L);
load()
方法
Customer customer = session.load(Custoemr.class,1L);
对象导航检索:Hibernate根据一个已经查询到的对象,获得其关联的对象的一种查询方式。
LinkMan linkMan = session.get(LinkMan.class,1L);
Customer customer = linkMan.getCustomer();
Customer customer = session.get(Customer.class,2L);
Set<LinkMan> linkMans = customer.getLinkMans();
Hibernate Query Language
,Hibernate的查询语言,是一种面向对象的方式的查询语言,语法类似SQL。通过session.createQuery()
,接收一个HQL进行查询方式。Query By Criteria
,条件查询。是一种更加面向对象化的查询方式。创建一个新项目,导入相关jar包
新建数据库,新建数据表,分别为cst_custoemr和cst_linkman
新建两个实体,分别为Customer和LinkMan
toString
方法,不包含LinkMantoString
方法,不包含Customer准备工具类,配置文件,映射文件
生成测试的数据,该方法执行3次
/**
* HQL的查询方式的测试类
* @author itzhouq
*
*/
public class HibernateDemo1 {
@Test
public void test1() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setCust_name("莫尔德");
for (int i = 0; i < 10; i++) {
LinkMan linkMan = new LinkMan();
linkMan.setLkm_name("老马"+i);
customer.getLinkMans().add(linkMan);
session.save(linkMan);
}
session.save(customer);
transaction.commit();
}
}
@Test
public void test2() {//HQL的简单查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 简单查询
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
// sql中支持*号的写法:select * from cst_customer;但是在HQL中不支持*号的写法。
// Query query2 = session.createQuery("select * from from Customer");// 报错
// List list2 = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test3() {// 别名查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 别名查询
// Query query = session.createQuery("from Customer c");
// List list = query.list();
Query query = session.createQuery("select c from Customer c");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test5() {// 排序查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 排序查询
// 默认情况
//List list = session.createQuery("from Customer order by cust_id").list();
// 设置兼降序排序 升序使用asc 降序使用desc
List<Customer> list = session.createQuery("from Customer order by cust_id desc").list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test4() {// 排序查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 排序查询
// 1. 按位置绑定:根据参数的位置进行绑定
// 一个条件
// Query query = session.createQuery("from Customer where cust_name = ?");
// query.setParameter(0, "朱元璋");
// 多个条件
// Query query = session.createQuery("from Customer where cust_source = ? and cust_name like ?");
// query.setParameter(0, "朋友推荐");
// query.setParameter(1, "朱%");
// 2. 按名称绑定
Query query = session.createQuery("from Customer where cust_source = :aaa and cust_name like :bbb");
// 设置参数:
query.setParameter("aaa", "朋友推荐");
query.setParameter("bbb", "朱%");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test6() {// 投影查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 投影查询
// 单个属性
// List
// for (Object customer : list) {
// System.out.println(customer);
//
// }
// 多个属性:
// List
// for (Object[] objects : list) {
// System.out.println(Arrays.toString(objects));
// }
// 查询多个属性,但我想封装到对象中-----需要这两个参数的构造函数
List<Object[]> list = session.createQuery("select c.cust_name,c.cust_source from Customer c").list();
for (Object[] customer : list) {
System.out.println(Arrays.toString(customer));
}
transaction.commit();
}
@Test
public void test7() {// 分页查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 分页查询
Query query = session.createQuery("from LinkMan");
query.setFirstResult(20);
query.setMaxResults(5);
List<LinkMan> list = query.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
transaction.commit();
}
@Test
public void test8() {// 分组统计查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 分组统计查询
Object object = session.createQuery("select count(*) from Customer").uniqueResult();
System.out.println(object);
//分组统计:
Query query = session.createQuery("select cust_source,count(*) from Customer group by cust_source");
List<Object[]> list = query.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
transaction.commit();
}
innner join(inner可以省略)
select * from A,B where A.id = B.aid;
select * from A inner join B on A.id = B.aid;
outer可以省略
select * from A left outer join B on A.id = B.id;
select * from A right outerjoin B on A.id = B.id;
@Test
public void test7() { // 多表查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// SQL:select * from cst_customer c inner join cst_linkman l on c.cust_id = l.lkm_cust_id
// HQL:内连接from Customer c inner join c.linkMans
// 内连接得到的是一个数组
// List
// for (Object[] objects : list) {
// System.out.println(Arrays.toString(objects));
// }
// HQL:迫切内连接,其实就是在普通的内连接inner join后添加一个关键字fetch.
// 迫切内连接的到的是一个对象,在Customer的toString中给定LinkMans可以得到全部字段
List<Customer> list2 = session.createQuery("select distinct c from Customer c inner join fetch c.linkMans").list();
for (Customer customer : list2) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test1() { // 简单查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
//获得Criteria的对象
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test2() { // 排序查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
//获得Criteria的对象
Criteria criteria = session.createCriteria(Customer.class);
// criteria.addOrder(Order.desc("cust_id")); // 降序
criteria.addOrder(Order.asc("cust_id")); // 升序
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test3() { // 分页查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
//获得Criteria的对象
Criteria criteria = session.createCriteria(LinkMan.class);
criteria.setFirstResult(20);
criteria.setMaxResults(5);
List<LinkMan> list = criteria.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
transaction.commit();
}
@Test
public void test4() { // 条件查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
//获得Criteria的对象
Criteria criteria = session.createCriteria(Customer.class);
// 设置条件
/*
* = eq
* > gt
* >= ge
* < lt
* <= le
* <> ne
* like
* in
* and
* or
*/
criteria.add(Restrictions.eq("cust_source", "小广告"));
criteria.add(Restrictions.like("cust_name", "李%"));
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test5() { // 条件查询
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
//获得Criteria的对象
Criteria criteria = session.createCriteria(Customer.class);
/**
* add :普通的条件。where后面条件
* addOrder:排序
* setProjecttion:聚合函数和group by having
*/
criteria.setProjection(Projections.rowCount());
Long num = (Long) criteria.uniqueResult();
System.out.println(num);
transaction.commit();
}
@Test
public void test6() { // 离线条件查询
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
detachedCriteria.add(Restrictions.like("cust_name", "李%"));
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
@Test
public void test1() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 调用的createSQLQuery,返回的值遍历后是数组形式的集合
// SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
// List
// for (Object[] objects : list) {
// System.out.println(Arrays.toString(objects));
// }
SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
sqlQuery.addEntity(Customer.class); //封装到实体Customer中
List<Customer> list = sqlQuery.list();
for (Customer customer : list) {
System.out.println(customer);
}
transaction.commit();
}
类级别的延迟加载
session.load(Customer.class,1L);
上的lazy进行配置。让lazy失效的方法有:
Hibernate.ininialize()
关联级别的延迟加载
指的是在查询到某个对象的时候,查询其关联对象的时候,是否采用延迟加载。
Customer customer = session.get(Customer.class,1L);
customer.getLinkMans();
//通过客户获得联系人的时候,联系人对象是否采用了延迟记载,称为关联级别的延迟加载。
抓取策略往往会和关联级别的延迟加载一起使用,优化语句。
Customer.hbm.xml
<set name="linkMans" fetch="select" lazy="true">
<key column="lkm_cust_id">key>
<one-to-many class="com.itzhouq.hibernate.domain.LinkMan"/>
set>
或者
上的fetch属性进行配置一批关联对象一起抓取,betch-size。
package com.itzhouq.hibernate.demo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.itzhouq.hibernate.domain.Customer;
import com.itzhouq.hibernate.domain.LinkMan;
import com.itzhouq.hibernate.utils.HibernateUtils;
/*
* 批量抓取测试
*/
public class HibernateDemo4 {
@Test
public void test1() {
// 获取客户的时候,批量抓取联系人
// 在Customer.hbm.xml中set上配置batch-size
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
List<Customer> list = session.createQuery("from Customer").list();
for (Customer customer : list) {
System.out.println(customer.getCust_name());
for (LinkMan linkMan : customer.getLinkMans()) {
System.out.println(linkMan.getLkm_name());
}
}
transaction.commit();
}
}
<set name="linkMans" batch-size="5">
<key column="lkm_cust_id">key>
<one-to-many class="com.itzhouq.hibernate.domain.LinkMan"/>
set>