(本文適用在struts+spring+hibernate3上做開(kāi)發(fā)的蟲(chóng)蟲(chóng)們)
類(lèi)名: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類(lèi)
* @author @家軍
* @param object POJO對(duì)象
* @param methodName 方法名稱(chēng)
* @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類(lèi)
* @author @家軍
* @param object POJO對(duì)象
* @param methodName 方法名稱(chēng)數(shù)組
* @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類(lèi)
* @author @家軍
* @param object POJO對(duì)象
* @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類(lèi)
* @author @家軍
* @param collection POJO對(duì)象集合
* @param methodName 方法名稱(chēng)數(shù)組
* @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類(lèi)
* @author @家軍
* @param collection POJO對(duì)象集合
* @param methodName 方法名稱(chēng)
* @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);
}
}
}
這個(gè)方法的好外是:可以不在hbm.xml的文件當(dāng)中,指定為lazy=true這個(gè)模式,可以直接使用。使用方法如下:
如果你使用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>
<!--配置基礎(chǔ)事務(wù)-->
<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(.);//取得你要展示的對(duì)象
//如果這個(gè)對(duì)象當(dāng)中有延遲加載的對(duì)象(SET)時(shí),則需要如下加載就行
this.hibernateUtil.initialize(what_ur_view, "getTbShipmentSale");
//其中g(shù)etTbShipmentSale是其對(duì)象(SET也可以操作) 在頁(yè)面顯示的時(shí)候,你就可以使用JSTL如下表述:
<c:out value="${what_ur_view.tbShipmentSale.goodsReceivePersonPhone}" />//呵呵,是不是很爽呀。
同樣的方法,我們也可以對(duì)一個(gè)SET在頁(yè)面進(jìn)行顯示,方法如下:
<c:forEach items="${what_ur_view.tbShipmentProductMappingSet}" var="ProductMapping" varStatus="status">
<c:out value="${ProductMapping.productNum}" />
<c:out value="${ProductMapping.tbOutOfWarehouse.outOfWarehouseNum}" />
</c:forEach>
//呵呵,支持多級(jí)嵌套,
在ACTION當(dāng)中則需要加入
hibernateUtil.initialize(what_ur_view.getTbShipmentProductMappingSet(),
new String[] { "getTbProduct", "getTbOutOfWarehouse",
"getTbProductConfigure" });