免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
hibernate的延遲加載通用方法
(本文適用在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" }); 
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)系列文章(2)——淺析VO、DTO、DO、PO的概念、區(qū)別和用處
getHibernateTemplate用法 hibernate
基于spring的DAO設(shè)計(jì)
hibernate 通用泛型DAO
J2EE開(kāi)發(fā)框架
hibernate在spring中的使用
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服