Hibernate 第四天,今天主要讲解的是关于Hiberante的查询总结
一、查询总结:
1.oid查询 get查询
2.对象属性导航查询
3.criteria查询
二、查询 -HQL语法
package com.hibernate.test.hql;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.hibernate.test.domain.Customer;
import com.hibernate.utils.HibnerateUtils;
//学习hql语法
public class Demo {
// 1.基本语法
@Test
public void fun1() {
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
// --------------------------
// 执行操作
String hql = "from Customer";
// 复杂写法
String hql2 = "from com.hibernate.test.domain.Customer";
String hql3 = "from java.lang.Object";
Query query = session.createQuery(hql3);
List list = query.list();
System.out.println(list);
// -------------------------
tx.commit();
session.close();
}
// 排序语法
// 条件
@Test
public void fun2() {
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
// --------------------------
// 执行操作
String hql = "from Customer order by cust_id desc";// 降序
String hql4 = "from Customer order by cust_id asc";// 升序
// 复杂写法
String hql2 = "from com.hibernate.test.domain.Customer";
String hql3 = "from java.lang.Object";
Query query = session.createQuery(hql);
List list = query.list();
System.out.println(list);
// -------------------------
tx.commit();
session.close();
}
// 分页
@Test
public void fun3() {
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
// --------------------------
// limit ?,?
String hql = "from Customer ";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(3);
List<Customer> list = query.list();
System.out.println(list);
// limit ?,?
// (当前页书-1)*每页条数
// -------------------------
tx.commit();
session.close();
}
// 统计查询
// count
// sunm
// avg
// max
// min
@Test
public void fun4() {
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
// --------------------------
// limit ?,?
// 求总记录数
String hql = "select count(*) from Customer ";
// id求和
String hql2 = "select sum(cust_id) from Customer ";
// 求id平均值
String hql3 = "select avg(cust_id) from Customer ";
// 求id的最大值
String hql4 = "select max(cust_id) from Customer ";
// 求id最小值
String hql5 = "select min(cust_id) from Customer ";
Query query = session.createQuery(hql5);
Number number = (Number) query.uniqueResult();
System.out.println(number);
// -------------------------
tx.commit();
session.close();
}
// 投影查询
@Test
public void fun5() {
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
// --------------------------
String hql="select cust_name from Customer";
//获取多个属性[‘
String hql2="select cust_name,cust_id from Customer";
//下边这个用到投影我们需要在相应实体类中添加如下的构造方法和空参构造方法。否则获取不到
String hql3="select new Customer(cust_id,cust_name) from Customer";
Query query=session.createQuery(hql3);
List list=query.list();
System.out.println(list);
//[百度, 黑马, 360安全, 雅虎, 蓝翔技工学院, 新东方, 传智学院]
// -------------------------
tx.commit();
session.close();
}
}
三、查询-Criteria语法
public class Demo3 {
// 离线查询
//
@Test
public void fun1() {
// web/service层
DetachedCriteria d = DetachedCriteria.forClass(Customer.class);
d.add(Restrictions.idEq(5l));
Session session = HibnerateUtils.opensession();
Transaction tx = session.beginTransaction();
Criteria a = d.getExecutableCriteria(session);
List<Customer> list = a.list();
for (Customer l : list) {
System.out.println(l.toString());
}
tx.commit();
session.close();
}
}
四、查询优化
1.类级别查询
session.get(Customer.class.5l);
get方法没有任何策略,调用即立即查询数据库加载数据
load方法会应用类级别的加载策略,加载会受 .hbm.xml文件下 <class name="com.hibernate.test.domain.Customer" table="CUSTOMER" lazy="true">
lazy属性的影响
lazy(默认值)true:擦汗寻时候会返回代理对象。执行懒加载,此代理对象会在使用属性时候,根据关联的session对象查询数据库加载数据
lazy:false load方法会与get房没有任何区别。调用时候立即加载。
结论:为了提高效率,建议使用load方法进行延迟加载,(懒加载)
注意:使用懒加载时候确保调用属性加载数据时,session还是打开的。否则会抛出异常。org.hibernate.LazyInitializationException: could not initialize proxy - no Session
2.关联界别查询
集合策略
关联属性策略
结论:为了提高效率,fetch的选择上应选择select lazy取值应选择true。