java

位置:IT落伍者 >> java >> 浏览文章

hibernate的延迟加载通用方法


发布日期:2020年08月09日
 
hibernate的延迟加载通用方法

类名HibernateUtil

package comantbeejeemodelutil;

import javalangreflectInvocationTargetException;

import javalangreflectMethod;

import javautilCollection;

import javautilIterator;

import orgspringframeworkormhibernatesupportHibernateDaoSupport;

public class HibernateUtil extends HibernateDaoSupport {

/**

* 初始化POJO类

* @author @家军

* @param object POJO对象

* @param methodName 方法名称

* @return

* @version

*/

public void initialize(Object object String methodName) throws SecurityException NoSuchMethodException IllegalArgumentException IllegalAccessException InvocationTargetException {

String[] methodArray = methodNamesplit(\\

Method method = null;

Object initializeObject = object;

if(methodArraylength == ){

thisgetHibernateTemplate()lock(initializeObject orghibernateLockModeNONE)

method = objectgetClass()getMethod(methodArray[] new Class[] {})

initializeObject = methodinvoke(initializeObject new Object[] {})

thisgetHibernateTemplate()initialize(initializeObject)

}else{

for(int i=;i<methodArraylength;i++){

method = initializeObjectgetClass()getMethod(methodArray[i] new Class[] {})

initializeObject = methodinvoke(initializeObject new Object[] {})

}

thisgetHibernateTemplate()lock(initializeObject orghibernateLockModeNONE)

thisgetHibernateTemplate()initialize(initializeObject)

}

}

/**

* 初始化POJO类

* @author @家军

* @param object POJO对象

* @param methodName 方法名称数组

* @return

* @version

*/

public void initialize(Object object String methodName[])

throws SecurityException NoSuchMethodException

IllegalArgumentException IllegalAccessException InvocationTargetException {

for (int i = ; i < methodNamelength; i++) {

String[] methodArray = methodName[i]split(\\

Method method = null;

Object initializeObject = object;

if(methodArraylength == ){

thisgetHibernateTemplate()lock(initializeObject orghibernateLockModeNONE)

method = objectgetClass()getMethod(methodArray[] new Class[] {})

initializeObject = methodinvoke(initializeObject new Object[] {})

thisgetHibernateTemplate()initialize(initializeObject)

}else{

for(int j=;j<methodArraylength;j++){

method = initializeObjectgetClass()getMethod(methodArray[j] new Class[] {})

initializeObject = methodinvoke(initializeObject new Object[] {})

}

thisgetHibernateTemplate()lock(initializeObject orghibernateLockModeNONE)

thisgetHibernateTemplate()initialize(initializeObject)

}

}

}

/**

* 初始化POJO类

* @author @家军

* @param object POJO对象

* @return

* @version

*/

public void initialize(Object object) throws SecurityException NoSuchMethodException IllegalArgumentException IllegalAccessException InvocationTargetException {

thisgetHibernateTemplate()lock(object orghibernateLockModeNONE)

thisgetHibernateTemplate()initialize(object)

}

/**

* 初始化POJO类

* @author @家军

* @param collection POJO对象集合

* @param methodName 方法名称数组

* @return

* @version

*/

public void initialize(Collection collection String methodName[])

throws SecurityException NoSuchMethodException

IllegalArgumentException IllegalAccessException InvocationTargetException {

for(Iterator i=erator()ihasNext()Wink{

Object object = inext()

thisinitialize(objectmethodName)

}

}

/**

* 初始化POJO类

* @author @家军

* @param collection POJO对象集合

* @param methodName 方法名称

* @return

* @version

*/

public void initialize(Collection collection String methodName)

throws SecurityException NoSuchMethodException

IllegalArgumentException IllegalAccessException InvocationTargetException {

for(Iterator i=erator()ihasNext()Wink{

Object object = inext()

thisinitialize(objectmethodName)

}

}

}

这个方法的好外是可以不在hbmxml的文件当中指定为lazy=true这个模式可以直接使用使用方法如下

如果你使用SPRING则需要把hibernateUtil注入其中

<bean id=hibernateUtilTarget class=comantbeejeemodelutilHibernateUtil>

<property name=sessionFactory>

<ref local=mssqlSessionFactory />

</property>

</bean>

<bean id=hibernateUtil parent=BaseTransactionProxy class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>

<property name=target>

<ref local=hibernateUtilTarget />

</property>

</bean>

<!配置基础事务>

<bean id=BaseTransactionProxy class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean 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 = XXXManagerfind(//取得你要展示的对象

//如果这个对象当中有延迟加载的对象(SET)时则需要如下加载就行

thishibernateUtilinitialize(what_ur_view getTbShipmentSale

//其中getTbShipmentSale是其对象(SET也可以操作)

在页面显示的时候你就可以使用JSTL如下表述

<c:out value=${what_ur_viewtbShipmentSalegoodsReceivePersonPhone} />//呵呵是不是很爽呀

同样的方法我们也可以对一个SET在页面进行显示方法如下

<c:forEach items=${what_ur_viewtbShipmentProductMappingSet} var=ProductMapping varStatus=status>

<c:out value=${ProductMappingproductNum} />

<c:out value=${ProductMappingtbOutOfWarehouseoutOfWarehouseNum} />

</c:forEach>

//呵呵支持多级嵌套

在ACTION当中则需要加入hibernateUtilinitialize(what_ur_viewgetTbShipmentProductMappingSet()

new String[] { getTbProduct getTbOutOfWarehouse

getTbProductConfigure })如果你要是在HIBERNATE当中使用这个就太简单了吧

怎么样?呵呵呵

从此我对STRUTS的标签深恶痛绝而对JSTL情有独钟了

               

上一篇:用struts+spring+hibernate组装web应用

下一篇:eclipse开发jface时,main.class解决方法