打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
hibernate的延迟加载通用方法
(本文适用在struts+spring+hibernate3上做开发的虫虫们)
类名:HibernateUtil

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注入其中:
 <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,则需要这样:
List what_ur_view = XXXManager.find(
.);
//取得你要展示的对象
//如果这个对象当中有延迟加载的对象(SET)时,则需要如下加载就行
this.hibernateUtil.initialize(what_ur_view, "getTbShipmentSale");
//其中getTbShipmentSale是其对象(SET也可以操作) 

在页面显示的时候,你就可以使用JSTL如下表述:
<c:out value="${what_ur_view.tbShipmentSale.goodsReceivePersonPhone}" />//呵呵,是不是很爽呀。 

同样的方法,我们也可以对一个SET在页面进行显示,方法如下:
<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当中则需要加入
hibernateUtil.initialize(what_ur_view.getTbShipmentProductMappingSet(),
new String[] { "getTbProduct""getTbOutOfWarehouse",
"getTbProductConfigure" }); 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
领域驱动设计系列文章(2)——浅析VO、DTO、DO、PO的概念、区别和用处
getHibernateTemplate用法 hibernate
基于spring的DAO设计
hibernate 通用泛型DAO
J2EE开发框架
hibernate在spring中的使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服