0 Replies Latest reply on Oct 29, 2006 7:56 PM by Chris Davies

    LazyInitializationException in strange circumstances

    Chris Davies Newbie

      Before posting about this problem, I should say that I'm new to JBoss. I've been spending the last 5 months learning Java EE and developing a system using Glassfish, but I've recently made the switch to JBoss 4.0.5 (with EJB3 capability).

      I've been having a number of problems cropping up during the migration, but this one's really got me confused. It seems that the LazyInitializationException crops up frequently (judging from the Google results and forum posts here) - however, it's normally in getter methods when people are using FetchType.LAZY.

      In my case, I seem to be getting this exception in a setter method when I'm using FetchType.EAGER. Here is the offending code:

       // ...
      
       @OneToMany( fetch = FetchType.EAGER, cascade = { CascadeType.ALL } )
       public Set< Season > getSeasons() { return m_seasons; }
       public void setSeasons( Set< Season > seasons )
       {
       m_seasons = new TreeSet< Season >();
      
       if( seasons != null )
       m_seasons.addAll( seasons );
       }
      
       // ...
      


      I'm using a TreeSet in this way because it allows me to preserve order when loading the entities back out of the database, as well as making use of the no-duplicates characteristic of sets. The actual line that causes the exception is the 'm_seasons.addAll( seasons );', as shown by this extract of the stack trace:


      ...

      Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
      at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
      at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
      at java.util.TreeSet.addAll(TreeSet.java:245)
      at uk.co.aeroglade.vv.entity.Property.setSeasons(Property.java:77)
      ... 93 more


      (the top-most exception, by the way, is "javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Exception occurred inside setter of uk.co.aeroglade.vv.entity.Property.seasons").

      All of this code was working perfectly on Glassfish - so I'm suspecting it has something to do with the architecture of JBoss that I've overlooked:

      This exception actually occurs inside a servlet which gets hold of a stateless EJB using JNDI lookup, since the @EJB annotation isn't supported by Tomcat 5.5. Once it has a reference to the EJB, it calls 'getAccounts', which returns an Account object. An Account object has a Property object, inside which has a collection of Season objects (as you can see above).

      The unhappiness seems to root from the fact that I'm using the parameter passed to the setter.

      I'll be honest: I'm completely confused and I'm not really sure where to dig for solutions to this. I'm determined to get this working though. Any guidance (explanations of why, references to articles/blog posts, etc.) would be greatly appreciated!


      Regards,

      Chris