(本文适用在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" }); |
|