熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

hibernate的延遲加載通用方法

2022-06-13   來源: Java開源技術 

  類名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情有獨鐘了


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28589.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.