知识点1: Session对象的生命周期与本地线程绑定
public void testCache() {
Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge());
System.out.println(customer);//测试散装数据
tx.commit();
session.close();
session=HibernateUtils.openSession();
tx=session.beginTransaction();
customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge());
//cn.itcast.c3p0.Customer@1551b0
System.out.println(customer); //测试散装数据
tx.commit();
session.close();
session=HibernateUtils.openSession();
tx=session.beginTransaction();
customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge());
//cn.itcast.c3p0.Customer@1758500
System.out.println(customer); //测试散列 是重组的对象
tx.commit();
session.close();
}
知识点9:测试集合级别的二级缓存(存放查询条件,即OID)
@Test
public void test(){
Session session=Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
//查询客户,关联集合,可以使用立即检索查看效果(2条sql语句)
Customer customer=(Customer)session.load(Customer.class, 1);
System.out.println(customer.getName());
System.out.println(customer.getOrders().size());
tx.commit();
session.close();
session=Session session=HibernateUtils.openSession();
tx=session.beginTransaction();
//不再显示sql语句,从二级缓存中获取
customer=(Customer)session.load(Customer.class, 1);
System.out.println(customer.getName());
System.out.println(customer.getOrders().size());
tx.commit();
session.close();
}
@test
public void testQuery(){
session= HibernateUtils.openSession();
tx=session.beginTransaction();
Query query=session.createQuery("from Customer where id=1");
query.list();
tx.commit();
session.close();
session=HibernateUtils.openSession();
tx=session.beginTransaction();
query=session.createQuery("from Customer where id=1");
query.list();
Customer c = (Customer)session.get(Customer.class,1);//不会查询数据库
c.getName();
tx.commit();
session.close();
}
public void testUpdate() {
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge());
customer.setAge(45);
tx.commit();
session.close();
session=sf.openSession();
tx=session.beginTransaction();
customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge()); //45
tx.commit();
session.close();
}
//测试缓存溢出存放到临时目录
@Test
public void testowerFlow() {
Session session=Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery(" from Order o");
//30个对象
query.list().size();
tx.commit();
session.close();
}
知识点15:时间戳缓存区域
public void testUpdateTimeStampCache() {
Session session=Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class, 4);//T1
System.out.println(customer.getAge());
//注意:不能使用set的方式更新数据,因为set的方式清理session的一级缓存和二级缓存没有关系
Query query=session.createQuery("update Customer c
set c.age=55 where c.id=4");
query.executeUpdate();//T2
tx.commit();
session.close();
session=Session session=HibernateUtils.openSession();
tx=session.beginTransaction();
//T2>T1重新查询数据库
customer=(Customer)session.load(Customer.class, 4);
System.out.println(customer.getAge());
tx.commit();
session.close();
}
知识点16:Query接口的iterate()方法
//iterator 先到数据库中检索符合条件的id,然后根据id分别到一级和二级缓冲中查找对象
//(没有在查询数据库,每次只能查一个,可能导致n+1次查询 )
public void testIterator() {
Session session=Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery(" from Order o where o.id<11");
query.list().size();
tx.commit();
session.close();
session=HibernateUtils.openSession();
tx=session.beginTransaction();
query=session.createQuery(" from Order o");
Iterator it=query.iterate();
while(it.hasNext()){
System.out.println(it.next().getOrderNumber());
}
tx.commit();
session.close();
}
public void testQueryCache() {
Session session=HibernateUtils.openSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery("select c from Customer c");
/*
* 设置查询缓存
* * 如果查询缓存存在 直接返回
* * 如果查询缓存不存在 查询数据库 将查询结果放置到查询缓存中
*/
query.setCacheable(true);//先不加,再添加,看效果
query.list();
tx.commit();
session.close();
Session session=Session session=HibernateUtils.openSession();
tx=session.beginTransaction();
query=session.createQuery("select c from Customer c");
query.setCacheable(true);//先不加,再添加,看效果
query.list();
tx.commit();
session.close();
}
package cn.test.hibernate3.demo1;
import java.util.HashSet;
import java.util.Set;
/**
* 客户的实体:
*
*/
public class Customer {
private Integer cid;
private String cname;
private Integer age;
// 乐观锁解决丢失更新,提供一个整数类型的属性.
private Integer ver;
public Customer() {
}
public Customer(Integer cid, String cname) {
super();
this.cid = cid;
this.cname = cname;
}
// 一个客户有多个订单.
private Set orders = new HashSet();
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getVer() {
return ver;
}
public void setVer(Integer ver) {
this.ver = ver;
}
@Override
public String toString() {
return "Customer [cid=" + cid + ", cname=" + cname + ", age=" + age
+ ", ver=" + ver + "]";
}
}
Order
package cn.test.hibernate3.demo1;
/**
* 订单的实体:
*
*/
public class Order {
private Integer oid;
private String addr;
// 订单属于某一个客户.放置一个客户的对象.
private Customer customer;
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Order [oid=" + oid + ", addr=" + addr + "]";
}
}
Customer.hbm.xml
from Customer
Order.hbm.xml
HibernateUtils
package cn.test.hibernate3.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate抽取工具类
*
*/
public class HibernateUtils {
private static Configuration configuration;
private static SessionFactory sessionFactory;
static {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
public static Session openSession() {
return sessionFactory.openSession();
}
public static Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public static void main(String[] args) {
openSession();
}
}
ehcache.xml
hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql:///hibernate3_day03
root
root
org.hibernate.dialect.MySQLDialect
true
true
false
update
4
thread
org.hibernate.connection.C3P0ConnectionProvider
5
20
120
3000
true
org.hibernate.cache.EhCacheProvider
true
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=off, stdout
HibernateTest1
package cn.test.hibernate3.demo1.test;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.demo1.Order;
import cn.test.hibernate3.utils.HibernateUtils;
/**
* Hibernate的检索方式的测试类:HQL
*
*/
public class HibernateTest1 {
@Test
// 初始化数据
public void demo1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCname("桐谷静香");
customer.setAge(35);
for (int i = 0; i < 3; i++) {
Order order = new Order();
order.setAddr("朝阳区" + i);
order.setCustomer(customer);
customer.getOrders().add(order);
session.save(order);
}
session.save(customer);
tx.commit();
session.close();
}
@Test
// HQL检索:简单查询
public void demo2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 简单查询
// List list = session.createQuery("from Customer").list();
List list = session
.createQuery("from Customer where cname=?")
.setParameter(0, "武藤兰").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
@Test
// 使用别名的检索方式
public void demo3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 使用as 起别名
// List list =
// session.createQuery("from Customer as c").list();
// as可以省略
// List list = session.createQuery("from Customer c").list();
// 条件中使用别名
// List list =
// session.createQuery("from Customer c where c.cname = ?").setParameter(0,
// "美美").list();
// HQL中不能select 后 写*号
List list = session.createQuery("select c from Customer c")
.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
@Test
// HQL 多态查询
// 可以是接口或某父类及其抽象类所有子类的查询
public void demo4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
List list = session.createQuery("from java.lang.Object").list();
for (Object object : list) {
System.out.println(object);
}
tx.commit();
session.close();
}
@Test
// 排序查询
public void demo5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
List list = session.createQuery(
"from Customer c order by c.cid desc").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
@Test
// 分页查询
public void demo6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Order");
query.setFirstResult(2);// 从哪开始 默认是0
query.setMaxResults(3); // 每页显示多少条
List list = query.list();
for (Order order : list) {
System.out.println(order);
}
tx.commit();
session.close();
}
@Test
// 唯一结果查询
public void demo7() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// Customer customer = (Customer) session
// .createQuery("from Customer where id=? ").setParameter(0, 1)
// .uniqueResult();
// System.out.println(customer);
Long count = (Long) session.createQuery("select count(*) from Order")
.uniqueResult();
System.out.println(count);
tx.commit();
session.close();
}
@Test
// 条件查询:
public void demo8() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 按位置绑定参数
// List list =
// session.createQuery("from Customer where cname = ?").setParameter(0,
// "武藤兰").list();
// List list
// =session.createQuery("from Customer where cname = ? and cid = ?")
// .setParameter(0, "武藤兰").setParameter(1, 2).list();
// 按名称绑定参数:
/*
* Query query = session
* .createQuery("from Customer where cname = :cname and cid = :cid");
* query.setParameter("cname", "桐谷静香"); query.setParameter("cid", 3);
*/
Query query = session.createQuery("from Customer where cname like ?");
query.setParameter(0, "%静%");
List list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
// 聚集函数查询:
public void demo9() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Integer id = (Integer) session.createQuery(
"select max(id) from Customer ").uniqueResult();
Long count = (Long) session.createQuery(
"select count(*) from Customer ").uniqueResult();
List
HibernateTest2
package cn.test.hibernate3.demo1.test;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.demo1.Order;
import cn.test.hibernate3.utils.HibernateUtils;
/**
* Hibernate检索方式测试类:QBC
* QBC: Query By Criteria
*/
public class HibernateTest2 {
@Test
// QBC:简单查询
public void demo1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询所有:
List list = session.createCriteria(Customer.class).list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
// QBC: 排序查询
public void demo2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询所有:
Criteria criteria = session.createCriteria(Order.class);
criteria.addOrder(org.hibernate.criterion.Order.desc("oid"));
List list = criteria.list();
for (Order order : list) {
System.out.println(order);
}
tx.commit();
session.close();
}
@Test
// 条件查询
public void demo3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
// 条件1
Criterion criterion1 = Restrictions.or(Restrictions.eq("cname", "武藤兰"),
Restrictions.eq("cid", 2));
Criterion criterion2 = Restrictions.or(criterion1,
Restrictions.like("cname", "%静%"));
criteria.add(criterion2);
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
@Test
// 分页查询
public void demo4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria=session.createCriteria(Order.class);
criteria.setFirstResult(2);
criteria.setMaxResults(3);
List list=criteria.list();
for (Order order : list) {
System.out.println(order);
}
tx.commit();
session.close();
}
@Test
// 离线条件查询:
public void demo5(){
DetachedCriteria criteria=DetachedCriteria.forClass(Customer.class);
criteria.add(Restrictions.eq("cname", "武藤兰"));
criteria.add(Restrictions.eq("cid", 1));
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
List list=criteria.getExecutableCriteria(session).list();
System.out.println(list);
tx.commit();
session.close();
}
}
HibernateTest3
package cn.test.hibernate3.demo1.test;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.utils.HibernateUtils;
/**
* Hibernate的查询方式:SQL
*
*/
public class HibernateTest3 {
@Test
public void demo1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/*
* List list =
* session.createSQLQuery("select * from customer") .list();
*
* for (Object[] objects : list) {
* System.out.println(Arrays.toString(objects)); }
*/
SQLQuery query = session.createSQLQuery("select * from customer");
query.addEntity(Customer.class);
List list=query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
session.close();
}
}
HibernateTest4
package cn.test.hibernate3.demo1.test;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.demo1.Order;
import cn.test.hibernate3.utils.HibernateUtils;
/**
* Hibernate的抓取策略
*
*
*/
public class HibernateTest4 {
@SuppressWarnings("unchecked")
@Test
// 批量抓取:
// 从订单批量抓取客户
// 在Customer.hbm.xml中标签上设置batch-size=""
public void demo9(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询所有的订单:
List list = session.createQuery("from Order").list();
for (Order order : list) {
System.out.println(order.getCustomer().getCname());
}
tx.commit();
session.close();
}
@Test
// 批量抓取:
// 从客户批量抓取订单
// 标签上设置batch-size=""
public void demo8(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询所有客户
List list = session.createQuery("from Customer").list();
for (Customer customer : list) {
for (Order order : customer.getOrders()) {
System.out.println(order.getAddr());
}
}
tx.commit();
session.close();
}
@Test
// 在Order.hbml.xml中标签上配置fetch和lazy
public void demo7(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/**
* fetch="select",lazy="false"
*/
// 查询一号订单
//Order order = (Order) session.get(Order.class, 1);// 发送多条SQL查询关联对象
// 查询一号订单所属客户的名称:
//System.out.println("客户名称:"+order.getCustomer().getCname());// 不发送SQL
/**
* fetch="select",lazy="proxy"
* proxy:查询关联对象的时候,是否采用延迟,由另一端的类级别延迟来决定.
* * Customer的上配置的是lazy="true".检索的时候采用延迟
* * Customer的上配置的是lazy="false".检索的时候不采用延迟
*/
// 查询一号订单
Order order = (Order) session.get(Order.class, 1);// 发送多条SQL查询关联对象
// 查询一号订单所属客户的名称:
System.out.println("客户名称:"+order.getCustomer().getCname());// 不发送SQL
tx.commit();
session.close();
}
@Test
// 在Order.hbml.xml中标签上配置fetch和lazy
public void demo6(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/**
* fetch="join",lazy被忽略.
*/
// 查询一号订单
Order order = (Order) session.get(Order.class, 1);// 发送一条迫切左外连接查询关联对象.
// 查询一号订单所属客户的名称:
System.out.println("客户名称:"+order.getCustomer().getCname());
tx.commit();
session.close();
}
@SuppressWarnings("unchecked")
@Test
// 在Customer.hbm.xml的标签上配置fetch和lazy
// fetch="subselect" 子查询
public void demo5(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/**
* 配置fetch="subselect" lazy="true"
*/
// List list = session.createQuery("from Customer").list();// 发送查询所有客户的SQL
// for (Customer customer : list) {
// System.out.println("客户订单数量:"+customer.getOrders().size());// 发送一个子查询去查询关联对象.
// }
// 使用子查询 查询多个的情况.
/*Customer customer = (Customer) session.get(Customer.class, 1);
System.out.println("客户订单数量:"+customer.getOrders().size());*/
/**
* 配置fetch="subselect" lazy="false"
*/
// List list = session.createQuery("from Customer").list();// 发送查询所有客户的SQL,发送一个子查询查询订单
// for (Customer customer : list) {
// System.out.println("客户订单数量:"+customer.getOrders().size());// 不发送SQL
// }
/**
* 配置fetch="subselect" lazy="extra"
*/
List list = session.createQuery("from Customer").list();// 发送查询所有客户的SQL
for (Customer customer : list) {
System.out.println("客户订单数量:"+customer.getOrders().size());// 只发送统计客户订单数量的sql
for (Order order : customer.getOrders()) {
System.out.println(order.getAddr());
}
}
tx.commit();
session.close();
}
@Test
// 在Customer.hbm.xml的标签上配置fetch和lazy
// fetch="select"
public void demo4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/**
* 配置fetch="select" lazy="true"
* * 发送多条SQL.默认值就是fetch="select" lazy="true"
*/
// 查询一号客户
// Customer customer = (Customer) session.get(Customer.class, 1);//发送一条查询客户的SQL
// 查看一号客户的订单的数量:
// System.out.println("订单的数量:" + customer.getOrders().size());//又发送一条查询订单的SQL
/**
* 配置fetch="select" lazy="false"
*/
// 查询一号客户
//Customer customer = (Customer) session.get(Customer.class, 1);//发送多条SQL查询
// 查看一号客户的订单的数量:
//System.out.println("订单的数量:" + customer.getOrders().size());//不发送SQL
/**
* 配置fetch="select" lazy="extra"
* * extra:及其懒惰的.
*/
// 查询一号客户
Customer customer = (Customer) session.get(Customer.class, 1);// 发送一条查询客户的SQL
// 查看一号客户的订单的数量:
System.out.println("订单的数量:" + customer.getOrders().size());// 查询订单的数量:select count(oid) from orders o where o.cid = ?
for (Order order : customer.getOrders()) {
System.out.println(order);
}
tx.commit();
session.close();
}
@Test
// 在Customer.hbm.xml的标签上配置fetch和lazy
// fetch="join",lazy的取值就会被忽略.
public void demo3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询一号客户
Customer customer = (Customer) session.get(Customer.class, 1);// 发送了一条迫切左外连接的SQL
// 查看一号客户的订单的数量:
System.out.println("订单的数量:" + customer.getOrders().size());// 不发送SQL.
tx.commit();
session.close();
}
@Test
// 默认的情况下 没有配置任何信息
// 发送多条SQL查询其关联对象.
public void demo2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 查询一号客户
Customer customer = (Customer) session.get(Customer.class, 1);// 发送一条查询客户的SQL.
// 查看一号客户的订单的数量:
System.out.println("订单的数量:" + customer.getOrders().size());// 使用订单的时候,又会发送一条SQL查询
tx.commit();
session.close();
}
@Test
// 区分立即和延迟检索
public void demo1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 立即检索
// Customer customer = (Customer) session.get(Customer.class, 1);
// System.out.println(customer.getCname());
// 延迟检索:
Customer customer = (Customer) session.load(Customer.class, 1);
Hibernate.initialize(customer);
System.out.println(customer.getCname());
tx.commit();
session.close();
}
}
HibernateTest5
package cn.test.hibernate3.demo1.test;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.utils.HibernateUtils;
//Hibernate的事务与并发的测试类:
public class HibernateTest5 {
@Test
public void demo9(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCname("李四");
session.save(customer);
tx.commit();
// session.close();
}
@Test
// 本地Session
public void demo8(){
Session session1 = HibernateUtils.getCurrentSession();
Session session2 = HibernateUtils.getCurrentSession();
System.out.println(session1 == session2);
}
@Test
// 本地Session
public void demo7(){
Session session1 = HibernateUtils.openSession();
Session session2 = HibernateUtils.openSession();
System.out.println(session1 == session2);
}
@Test
// 乐观锁解决丢失更新:
public void demo6(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class,3);
customer.setAge(36);
tx.commit();
session.close();
}
@Test
// 乐观锁解决丢失更新:
public void demo5(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class,3);
customer.setCname("铁男");
tx.commit();
session.close();
}
@SuppressWarnings("deprecation")
@Test
// 悲观锁解决丢失更新:
public void demo4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class,3,LockMode.UPGRADE);
customer.setAge(36);
tx.commit();
session.close();
}
@SuppressWarnings("deprecation")
@Test
// 悲观锁解决丢失更新:
public void demo3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class,3,LockMode.UPGRADE);
customer.setCname("铁男");
tx.commit();
session.close();
}
@Test
// 演示丢失更新
public void demo2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 3);
customer.setAge(36);
tx.commit();
session.close();
}
@Test
// 演示丢失更新
public void demo1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 3);
customer.setCname("铁男");
tx.commit();
session.close();
}
}
HibernateTest6
package cn.test.hibernate3.demo1.test;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.test.hibernate3.demo1.Customer;
import cn.test.hibernate3.demo1.Order;
import cn.test.hibernate3.utils.HibernateUtils;
public class HibernateTest6 {
@Test
// 查询缓存的测试
public void demo9(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select c.cname from Customer c");
// 使用查询缓存:
query.setCacheable(true);
query.list();
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
query = session.createQuery("select c.cname from Customer c");
query.setCacheable(true);
query.list();
tx.commit();
}
@SuppressWarnings("unused")
@Test
// 更新时间戳
public void demo8(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 2);
session.createQuery("update Customer set cname = '奶茶' where cid = 2").executeUpdate();
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
Customer customer2 = (Customer) session.get(Customer.class, 2);
tx.commit();
}
@SuppressWarnings("all")
@Test
// 将内存中的数据写到硬盘
public void demo7(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
List list = session.createQuery("from Order").list();
tx.commit();
}
@Test
// 一级缓存的更新会同步到二级缓存:
public void demo6(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);
customer.setCname("芙蓉");
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
Customer customer2 = (Customer) session.get(Customer.class, 1);
tx.commit();
}
@SuppressWarnings("unchecked")
@Test
// iterate()方法可以查询所有信息.
// iterate方法会发送N+1条SQL查询.但是会使用二级缓存的数据
public void demo5(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// N+1条SQL去查询.
Iterator iterator = session.createQuery("from Customer").iterate();
while(iterator.hasNext()){
Customer customer = iterator.next();
System.out.println(customer);
}
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
iterator = session.createQuery("from Customer").iterate();
while(iterator.hasNext()){
Customer customer = iterator.next();
System.out.println(customer);
}
tx.commit();
}
@SuppressWarnings("unchecked")
@Test
// 查询所有.Query接口的list()方法.
// list()方法会向二级缓存中放数据,但是不会使用二级缓存中的数据.
public void demo4(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 查询所有客户:
// list方法会向二级缓存中放入数据的.
List list = session.createQuery("from Customer").list();
for (Customer customer : list) {
System.out.println(customer.getCname());
}
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
// Customer customer = (Customer) session.get(Customer.class, 1);// 没有发生SQL ,从二级缓存获取的数据.
// list()方法没有使用二级缓存的数据.
list = session.createQuery("from Customer").list();
for (Customer customer : list) {
System.out.println(customer.getCname());
}
tx.commit();
}
@Test
// 二级缓存的集合缓冲区特点:
public void demo3(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);
// 查询客户的订单.
System.out.println("订单的数量:"+customer.getOrders().size());
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
Customer customer2 = (Customer) session.get(Customer.class, 1);
// 查询客户的订单.
System.out.println("订单的数量:"+customer2.getOrders().size());
tx.commit();
}
@SuppressWarnings("unused")
@Test
// 配置二级缓存的情况
public void demo2(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer1 = (Customer) session.get(Customer.class, 1);// 发送SQL.
Customer customer2 = (Customer) session.get(Customer.class, 1);// 不发送SQL.
System.out.println(customer1 == customer2);
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
Customer customer3 = (Customer) session.get(Customer.class, 1);// 不发送SQL.
Customer customer4 = (Customer) session.get(Customer.class, 1);// 不发送SQL.
System.out.println(customer3 == customer4);
tx.commit();
}
@SuppressWarnings("unused")
@Test
// 没有配置二级缓存的情况
public void demo1(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer1 = (Customer) session.get(Customer.class, 1);// 发送SQL.
Customer customer2 = (Customer) session.get(Customer.class, 1);// 不发送SQL.
tx.commit();
session = HibernateUtils.getCurrentSession();
tx = session.beginTransaction();
Customer customer3 = (Customer) session.get(Customer.class, 1);// 发送SQL.
tx.commit();
}
}