学习hibernate的源代码-------对象属性延迟加载

本文通过自定义实现展示了Hibernate中对象属性的延迟加载机制。利用JavaAssist生成代理对象,在需要时才从“数据库”加载数据,实现了高效的数据访问。

自己参考hibernate的源代码,写了个大概能演示hibernate对对象熟悉延迟加载的几个类,用到了javassist 包。

 

  调用的接口是这样的:

 

public class Test {
	public static void main(String[] args) {
		HibernateSeesion session = new HibernateSeesion();
		
		Custerinform c1 = (Custerinform)session.load(Custerinform.class, "2");
		//延迟加载对象上除了id以外的熟悉,无需连接数据库
		System.out.println(c1.getId());									
		
		//正在需要用到其他熟悉时,再去数据库加载数据
		System.out.println(c1.getFirstName());		
		
		//复用已经完全加载出来的对象,无需再连接数据库
		System.out.println(c1.getLastName());		
	}
}
 

 

 

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class HibernateSeesion {
	
	private static Map<String,Object> memDataBase = new HashMap<String,Object>();	//mock database
	
	//一个标识接口,为每个被代理的对象都加上这个接口
	private static Class<?>[] proxyInterfaces  = {HibernateProxy.class};
	
	//proxy工厂
	private static Class<?> proxyFactory;
	
	static{
		
		Custerinform cust1 = new Custerinform();
		cust1.setId("1");
		cust1.setFirstName("张");
		cust1.setLastName("阳");
		
		Custerinform cust2 = new Custerinform();
		cust2.setId("2");
		cust2.setFirstName("张");
		cust2.setLastName("梓琳");
		
		memDataBase.put(Custerinform.class.getName() + cust1.getId(), cust1);
		memDataBase.put(Custerinform.class.getName() + cust2.getId(), cust2);
		//这里的写法,只是为了简单模拟一下hibernate的机制而已
		proxyFactory = LazyInitializer.getProxyFactory(Custerinform.class,proxyInterfaces);
	}
	
	
	/**
	 * 返回proxy对象
	 * @param theClass
	 * @param id
	 * @return
	 */
	public Object load(Class<?> theClass, Serializable id){
		return LazyInitializer.getProxy(proxyFactory, this, Custerinform.class.getName(),id);
	}
	
	/**
	 * 模拟从数据库中即使加载
	 * @param entityName
	 * @param id
	 * @return
	 */
	public Object immediateLoad(String entityName, Serializable id){
		return memDataBase.get(entityName+id);
	}
}
 

 

   下面这个类是整个延迟加载的核心类容,

    proxyfatory 和 proxy 都是在这里生成的,借助了  javassist 包提供的api。

 

import java.io.Serializable;
import java.lang.reflect.Method;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;

public class LazyInitializer implements MethodHandler {

	private static final String INVOKE = "INVOKE";

	private boolean constructed = false;
	private boolean initialized = false;
	private Serializable id;
	private Object target;
	private HibernateSeesion session;
	private String entityName;

	public LazyInitializer(final HibernateSeesion session, final String entityName, final Serializable id) {
		this.id = id;
		this.session = session;
		this.entityName = entityName;
	}

	public static Class<?> getProxyFactory(final Class<?> persistentClass, Class<?>[] interfaces) {
		try {
			ProxyFactory factory = new ProxyFactory();

			factory.setSuperclass(persistentClass);

			factory.setInterfaces(interfaces);

			return factory.createClass();
		} catch (Throwable t) {
			throw new RuntimeException("Javassist Enhancement failed: " + persistentClass.getName(), t);
		}
	}

	public static HibernateProxy getProxy(final Class<?> factory, final HibernateSeesion session,
			final String entityName, final Serializable id) {
		final LazyInitializer instance = new LazyInitializer(session, entityName, id);

		HibernateProxy proxy;

		try {
			proxy = (HibernateProxy) factory.newInstance();
		} catch (Exception e) {
			throw new RuntimeException("Javassist Enhancement failed: " + entityName, e);
		}

		((ProxyObject) proxy).setHandler(instance);

		instance.constructed = true;

		return proxy;
	}

	@Override
	public Object invoke(Object proxy, Method thisMethod, Method proceed, Object[] args) throws Throwable {
		if (this.constructed) {

			Object result;

			final Object returnValue;

			result = this.invoke(thisMethod, args, proxy); //判断被调用的方法是不是getId

			if (result == INVOKE) {

				Object target = getInvoke(); //获取目标对象

				returnValue = thisMethod.invoke(target, args); //在目标对象上调用方法

				return returnValue == target ? proxy : returnValue;

			} else {
				return result;
			}

		} else {
			return proceed.invoke(proxy, args);
		}
	}

	public final Serializable getIdentifier() {
		return id;
	}

	public final void setIdentifier(Serializable id) {
		this.id = id;
	}

	public final Object getInvoke() {
		initialize();
		return target;
	}

	/**
	 *  拦截 getId 方法,如果只是请求这个方法的话,无需从数据库中加载数据 
	 */
	private final Object invoke(Method method, Object[] args, Object proxy) {

		String methodName = method.getName();
		int params = args.length;

		if (params == 0) {
			if (methodName.equals("getId")) {
				return getIdentifier();
			}
		}

		return INVOKE;
	}

	/**
	 * 初始化代理对象的目标对象
	 */
	private final void initialize() {

		if (!initialized) {

			System.out.println("load data from database");

			target = session.immediateLoad(entityName, id);

			initialized = true;
		} else {
			checkTargetState();
		}
	}

	private void checkTargetState() {
		if (target == null) {
			System.out.println("找不到数据");
		}
	}
}
 

 

 

public interface HibernateProxy {

}
 

 

public class Custerinform {
	private String id;
	
	private String firstName;
	
	private String lastName;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}
本下载包与书籍的实战篇紧密结合. 下载包中的程序是一个简明的示例论坛程序,按照循序渐进的原则,分为不同的步骤: 步骤1: 建立最简单的项目基础结构:Board/User/Article三个类,包含最基本的配置文件。同时,在这一步中,建立测试类,刻画项目各个类之间的契约关系。 步骤2: 对文章的种类增加“投票”类型,展示对多型的处理。 步骤3: 增加webwork实现的GUI。 加入中文支持。 步骤4: 升级到Hibernate 3。 环境说明 --------- 程序包中的程序经过测试的运行环境为: 1, Tomcat 5.0.25 2, Hibernate 2.1.7 3, Hibernate 3.0.2 4, Eclipse 3.0.1 (作为IDE) 5, ant 1.6(作为build工具) 6, java sdk 1.4.2 7, mysql 4.1 (作为后台数据库) 8, 至少250M硬盘空间(hibernate 2,3和webwork就将占据138M) 虽然在更高版本中应该可以不加修改的运行,但我们对此不加以保证。(v1.2将对Hibernate 3.0.4进行测试) 安装说明 --------- 1, 确认jdk 1.4.2已经正确安装; 2,将本程序包解压,假设目录为: x:\rs\hib-samples 3, 从hibernate的sf下载区下载2.1.7与3.0.2版本: http://sourceforge.net/project/showfiles.php?group_id=40712 4,将下载的hibernate包解压到x:\rs\hib-samples\hibernate-2.1与x:\rs\hib-samples\hibernate-3,目录结构应该为: x: + rs + hib-samples <--- 本文件包解压的目录 + forum-step1-db-first-middlegen + forum-step1-db-first-synchronizer + forum-step2 + ... + hibernate-2.1 <--- Hibernate 2.1.7解压到这里 + bin <--- 确认bin与doc目录就在解压后的hibernate-2.1目录中 + doc + ... + hibernate-3.0 <--- Hibernate 3.0.2解压到这里 + bin preference -> Java -> Build Path -> User Librarys,分别建立名为Hibernate2,hibernate3,mysql-jdbc的用户库,分别包含对应的lib文件(如hibernate2包含hibernate-2.1目录下的hibernate2.jar及其lib目录下的所有jar文件); 11, 在您的eclipse 中,导入各个目录下的.project文件,以建立工程.(工程名为step1,step2,step3,step4)。在工程的类路径配置中,使用上一步创建的用户库(step1-step3使用Hibernate2,step4使用hibernate3)。确保没有编译错误。 好了,您现在可以开始阅读代码,作一些自己的改变,看看您能得到什么结果 :) 中文处理说明 ------------ 关于中文的处理,是在step3加入的,因此在step1以及step2中,请使用英文进行实验。具体进行的处理为全程采用UTF-8编码. 1,mysql创建时,字符集必须选择UTF-8 2,在mysql jdbc连接的url中,必须指定采用utf-8 encoding。 jdbc:mysql://localhost/forum?useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8 3,在jsp页面中,指定页面采用UTF-8编码. 假若在您的项目中,必须使用GBK编码,则在以上的各个地方,都需要把UTF-8更换为GBK方可正常使用。 版本更新 ----------- v1.2 (PLAN,TBD) * 转换到JTA * 给出一个脱离DAO模式的例子 * 给出for hibernate 3的xdoclet例子 v1.1 (2005.5.25) * 分离db目录下的build.xml * 增加了对投票的图形显示 * 修正了和webwork相关的一个中文bug v1.0 (2005.4.20) * 初始版本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值