hibernate对象状态的转换,延迟检索

package com.tudou.hibernates.t1;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TestGet {
	private static Configuration cfg = new Configuration().configure();
	private static SessionFactory fac = cfg.buildSessionFactory();
	private static Session son = fac.openSession();

	// 创建临时状态
	private static void testCreateTran() {
		Customer c = new Customer("小三");
	}

	// 直接获得持久化状态
	private static void testCreatePer() {
		// 获得实例对象
		Customer c = (Customer) son.load(Customer.class, 1);
		// 获取属性时会从数据库select初始化对象 默认为延时加载
		System.out.println(c.getCusName());
	}

	// 获得持久化状态固化至数据库
	private static void testCreatePer1() {
		Customer c = new Customer("小三");
		Transaction tran = son.beginTransaction();
		son.save(c);

		tran.commit();
		son.close();
		fac.close();
	}

	// 获得持久化状态并更改属性固化至数据库
	private static void testCreatePer2() {
		Customer c = new Customer("小三");
		Transaction tran = son.beginTransaction();
		c.setCusName("小4");

		son.save(c);
		tran.commit();
		son.close();
		fac.close();
	}

	// 手动构造游离状态
	private static void testCreateDe() {
		Customer c = new Customer();
		c.setCid(3);// 数据库中必须事先存在 否则后面更新
		c.setCusName("土豆");
		Transaction tran = son.beginTransaction();
		son.update(c);
		c.setCusName("土");
		tran.commit();
		son.close();
		fac.close();
	}

	/**
	 * 以下实现三种状态间的相互转换
	 */

	// 由临时变为持久状态
	private static void testFromTranToPer() {
		Customer c = new Customer();
		c.setCid(3);// 数据库中必须事先存在 否则后面更新
		c.setCusName("土豆");
		son.save(c);// 没有提交但是是持久状态
	}

	// 由持久变为游离状态
	private static void testFromPerToDe() {
		Customer c = (Customer) son.load(Customer.class, 1);
		son.close();// 关闭后变为游离
	}

	// 由游离变持久状态
	private static void testFromDeToPer() {
		Customer c = (Customer) son.load(Customer.class, 1);
		son.close();// 关闭后变为游离
		SessionFactory fac = cfg.buildSessionFactory();
		Session son1 = fac.openSession();// 要重新打开
		Transaction tran = son1.beginTransaction();
		son1.update(c);// update使对象重新变为持久状态 使用update后,可以修改属性 注:但不能在它之前修改!!!
		c.setCusName("豆豆");
		// tran.commit();
	}

	// 3种状态的相互转换
	private static void testFromDeToPerToTran() {
		Customer c = new Customer();// 临时
		c.setCusName("gg");
		Transaction tran1 = son.beginTransaction();
		son.save(c);// 持久
		tran1.commit();// 固化至数据库
		son.close();// 关闭后变为游离

		SessionFactory fac = cfg.buildSessionFactory();
		Session son1 = fac.openSession();// 要重新打开
		Transaction tran = son1.beginTransaction();
		son1.update(c);// update使游离对象重新变为持久状态
		c.setCusName("豆豆");
		tran.commit();
		son1.close();
	}

	// 不能存在两个id相同的对象 清除其中一个。
	private static void testEvi() {
		Transaction tran = son.beginTransaction();
		Customer c1 = (Customer) son.load(Customer.class, 1);
		Customer c = new Customer();
		c.setCid(1);
		c.setCusName("你管我谁");
		son.evict(c1);// 必须先清除一个
		son.update(c);
		tran.commit();
	}

	// 不能存在两个id相同的对象 清除其中一个。
	private static void testEviFlush() {
		Transaction tran = son.beginTransaction();
		for (int i = 0; i < 10000; i++) {
			Customer c = new Customer();
			c.setCusName("用户" + i);
			son.save(c);
			System.out.println(c.getCusName());
			if (i % 20 == 0) {
				son.flush();// 刷新缓存
				son.clear();// 清除缓存
			}
		}
		// tran.commit();
	}

	// 类级别立即检索 lazy="false";
	private static void testQuery() {
		Customer c1 = (Customer) son.load(Customer.class, 1);// 此时查询数据库,控制台出现sql语句
		// System.out.println(c1.getCusName());
	}

	// 类级别延迟检索 lazy="true";
	private static void testQuery1() {
		// 此时并不查询数据库,仅实例化对象,这个对象有id
		Customer c1 = (Customer) son.load(Customer.class, 1);
		// 对象有id,它不会查询数据库,控制台无sql语句
		System.out.println(c1.getCid());
		// 此时查询数据库,控制台出现sql语句
		System.out.println(c1.getCusName());
	}

	// 类级别延迟检索 lazy="true";
	private static void testQuery2() {
		// 此时并不查询数据库,仅实例化对象,这个对象有id
		Customer c1 = (Customer) son.load(Customer.class, 1);
		son.close();
		// 因session关闭,此时出现错误
		System.out.println(c1.getCusName());
	}

	// 类级别延迟检索 lazy="true";
	private static void testQuery3() {
		// 此时并不查询数据库,仅实例化对象,这个对象有id
		Customer c1 = (Customer) son.load(Customer.class, 1);
		// 显式的初始化代理实例,可以为对象,也可以为它的属性就去,getId除外,否则下面访问时将出现错误
		Hibernate.initialize(c1.getCusName());
		son.close();
		// 显式的初始化代理实例后,应用程序第一次访问时它将从数据库加载所有数据,控制台出现sql语句,但访问它的id时除外。
		System.out.println(c1.getCusName());
	}

	// 迫切左外连接 out-join="true"
	private static void testQuery4() {
		Customer c1 = (Customer) son.load(Customer.class, 1);
		// 配置Customer中的orders, 一对多。此时可以在下面getOrders访问oders对象,此处省略
	}

	public static void main(String[] args) {
		testQuery3();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值