1 Reply Latest reply on Nov 25, 2006 4:10 AM by Andrew Rubinger

    Catching Throwable does not catch LazyInitializationExceptio

    Michael JOnes Newbie

      I have an entity class with a toString() method but it is failing with a LazyInitializationException.

      How can isDAOAccessable() thrown an exception if it is catching Throwable? I am using jboss-4.0.5.GA


      @Entity
      @Table(name = "STOCKITEM")
      @IdClass(ProductKey.class)
      public class Product implements Serializable {
      @Override
      public String toString()
      {
      String desc = "PRODUCTCODE:=" + this.getProductCode() + ", TITLE:=" + title +
      ", RICHDESCRIPTION:=" + richDescription + ", ICONSSTR:=" + iconsstr + ", ORDER_LIMIT_QTY:=" +
      maximumItemPerOrder + ", PRODUCTVARIANTS:=[\n";
      if ( CommonUtils.isDAOAccessable(productVariants))
      for(ProductVariant pVar : productVariants)
      desc += pVar.toString() + "\n";
      else
      desc += "null";

      }
      }


      /**
      * Checks if a Collection returned using Hibernate has been loaded or
      * if it will cause a lazy loaded exception
      * @param c
      * @return
      */
      public static boolean isDAOAccessable(Collection c) {
      try {
      System.out.println("WORD UP");
      if (c==null) {
      return false;
      }
      else {
      c.iterator().hasNext();
      return true; // It is ok
      }
      }
      catch (Throwable ex){
      return false;
      }
      }


      This is the result

      19:26:23,898 INFO [STDOUT] WORD UP
      19:26:23,898 ERROR [LazyInitializationException] failed to lazily initialize a c
      ollection of role: estore.common.Product.productVariants, no s
      ession or session was closed
      org.hibernate.LazyInitializationException: failed to lazily initialize a collect
      ion of role: estore.common.Product.productVariants, no session
      or session was closed
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
      lizationException(AbstractPersistentCollection.java:358)
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
      lizationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
      at org.hibernate.collection.AbstractPersistentCollection.initialize(Abst
      ractPersistentCollection.java:343)
      at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPe
      rsistentCollection.java:86)
      at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:16
      3)
      at estore.common.util.CommonUtils.isDAOAccessable(Ljav
      a.util.Collection;)Z(Unknown Source)
      at estore.common.Product.toString()Ljava.lang.String;(
      Unknown Source)
      at org.apache.log4j.or.DefaultRenderer.doRender(DefaultRenderer.java:26)

      at org.apache.log4j.or.RendererMap.findAndRender(RendererMap.java:70)
      at org.apache.log4j.spi.LoggingEvent.getRenderedMessage(LoggingEvent.jav
      a:288)
      at org.apache.log4j.helpers.PatternParser$BasicPatternConverter.convert(
      PatternParser.java:395)
      at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.jav
      a:56)
      at org.apache.log4j.PatternLayout.format(PatternLayout.java:495)
      at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292)
      at org.apache.log4j.WriterAppender.append(WriterAppender.java:150)