本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7586960
建立时间:2006年5月29日




[Hibernate]hibernate的延迟加载通用方法
软件技术

lhwork 发表于 2006/7/21 11:23:02

呵呵,在忙一个项目,潜水很久,现在冒个泡:(本文适用在struts+spring+hibernate3上做开发的虫虫们) 类名:HibernateUtil Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->package com.antbee.j2eemodel.util;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Collection;import java.util.Iterator;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class HibernateUtil extends HibernateDaoSupport {/*** 初始化POJO类* @author @家军* @param object POJO对象* @param methodName 方法名称* @return* @version 1.0*/public void initialize(Object object, String methodName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {String[] methodArray = methodName.split("\\.");Method method = null;Object initializeObject = object;if(methodArray.length == 1){this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);method = object.getClass().getMethod(methodArray[0], new Class[] {});initializeObject = method.invoke(initializeObject, new Object[] {});this.getHibernateTemplate().initialize(initializeObject);}else{for(int i=0;i<methodArray.length;i++){method = initializeObject.getClass().getMethod(methodArray[i], new Class[] {});initializeObject = method.invoke(initializeObject, new Object[] {});}this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);this.getHibernateTemplate().initialize(initializeObject);}}/*** 初始化POJO类* @author @家军* @param object POJO对象* @param methodName 方法名称数组* @return* @version 1.0*/public void initialize(Object object, String methodName[])throws SecurityException, NoSuchMethodException,IllegalArgumentException, IllegalAccessException, InvocationTargetException {for (int i = 0; i < methodName.length; i++) {String[] methodArray = methodName[i].split("\\.");Method method = null;Object initializeObject = object;if(methodArray.length == 1){this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);method = object.getClass().getMethod(methodArray[0], new Class[] {});initializeObject = method.invoke(initializeObject, new Object[] {});this.getHibernateTemplate().initialize(initializeObject);}else{for(int j=0;j<methodArray.length;j++){method = initializeObject.getClass().getMethod(methodArray[j], new Class[] {});initializeObject = method.invoke(initializeObject, new Object[] {});}this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);this.getHibernateTemplate().initialize(initializeObject);}}}/*** 初始化POJO类* @author @家军* @param object POJO对象* @return* @version 1.0*/public void initialize(Object object) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {this.getHibernateTemplate().lock(object, org.hibernate.LockMode.NONE);this.getHibernateTemplate().initialize(object);}/*** 初始化POJO类* @author @家军* @param collection POJO对象集合* @param methodName 方法名称数组* @return* @version 1.0*/public void initialize(Collection collection, String methodName[])throws SecurityException, NoSuchMethodException,IllegalArgumentException, IllegalAccessException, InvocationTargetException {for(Iterator i=collection.iterator();i.hasNext()Wink{Object object = i.next();this.initialize(object,methodName);}}/*** 初始化POJO类* @author @家军* @param collection POJO对象集合* @param methodName 方法名称* @return* @version 1.0*/public void initialize(Collection collection, String methodName)throws SecurityException, NoSuchMethodException,IllegalArgumentException, IllegalAccessException, InvocationTargetException {for(Iterator i=collection.iterator();i.hasNext()Wink{Object object = i.next();this.initialize(object,methodName);}}}  这个方法的好外是:可以不在hbm.xml的文件当中,指定为lazy=true这个模式,可以直接使用。使用方法如下: 如果你使用SPRING,则需要把hibernateUtil注入其中: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> <bean id="hibernateUtilTarget" class="com.antbee.j2eemodel.util.HibernateUtil"><property name="sessionFactory"><ref local="mssqlSessionFactory" /></property></bean><bean id="hibernateUtil" parent="BaseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="target"><ref local="hibernateUtilTarget" /></property></bean><!--配置基础事务--><bean id="BaseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"><property name="transactionManager"><ref bean="mssqltransactionManager" /></property><property name="proxyTargetClass"><value>true</value></property><property name="transactionAttributes"><props><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean>  使用示例: 如果你使用STRUTS,则需要这样: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->List what_ur_view = XXXManager.find(500)this.width=500'>500)this.width=500'>500)this.width=500'>.);//取得你要展示的对象//如果这个对象当中有延迟加载的对象(SET)时,则需要如下加载就行this.hibernateUtil.initialize(what_ur_view, "getTbShipmentSale");//其中getTbShipmentSale是其对象(SET也可以操作)  在页面显示的时候,你就可以使用JSTL如下表述: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><c:out value="${what_ur_view.tbShipmentSale.goodsReceivePersonPhone}" />//呵呵,是不是很爽呀。  同样的方法,我们也可以对一个SET在页面进行显示,方法如下: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><c:forEach items="${what_ur_view.tbShipmentProductMappingSet}" var="ProductMapping" varStatus="status"><c:out value="${ProductMapping.productNum}" /><c:out value="${ProductMapping.tbOutOfWarehouse.outOfWarehouseNum}" /></c:forEach>//呵呵,支持多级嵌套,  在ACTION当中则需要加入Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->hibernateUtil.initialize(what_ur_view.getTbShipmentProductMappingSet(),new String[] { "getTbProduct", "getTbOutOfWarehouse","getTbProductConfigure" }); 呵,如果你要是在HIBERNATE当中使用,这个就太简单了吧。 怎么样?呵呵呵, 从此,我对STRUTS的标签深恶痛绝,而对JSTL情有独钟了。


阅读全文(2708) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.043 second(s), page refreshed 144754436 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号