title: LazyInitializationException
body: <p>I'm getting this exceptions:</p>
  
  <pre><code>javax.el.ELException: Error reading 'id' on type com.example.model.Article_$$_javassist_2
  ...
  
  org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
  org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
  org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
  com.example.model.Article_$$_javassist_2.getId(Article_$$_javassist_2.java)
  ...
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>@Entity
  @Table( name = "tbl_articles" )
  public class Article implements Comparable&lt;Article&gt;, Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column( nullable = false )
      @GeneratedValue( strategy = GenerationType.IDENTITY )
      private Integer id;
  
      // some other fields
  
      @ManyToMany( cascade = { CascadeType.ALL } )
      @JoinTable( name = "tbl_articles_categories",
          joinColumns = { @JoinColumn( name = "article_id" ) },
          inverseJoinColumns = { @JoinColumn( name = "category_id" ) })
      @ForeignKey( name = "tbl_articles_categories_fkey_article",
          inverseName = "tbl_articles_categories_fkey_category" )
      private Set&lt;Category&gt; categories = new HashSet&lt;Category&gt;();
  
      @ManyToMany( cascade = { CascadeType.ALL } )
      @JoinTable( name = "tbl_articles_tags",
          joinColumns = { @JoinColumn( name = "article_id" ) },
          inverseJoinColumns = { @JoinColumn( name = "tag_id" ) })
      @ForeignKey( name = "tbl_articles_tags_fkey_article",
          inverseName = "tbl_articles_tags_fkey_tag" )
      private Set&lt;Tag&gt; tags = new HashSet&lt;Tag&gt;();
  
      // getters and setters
  }
  
  public abstract class BaseService&lt;E, D extends BaseDAO&lt;E&gt;&gt;
  {
      protected D dao;
  
      public BaseService()
      {
      }
  
      protected D getDao()
      {
          return dao;
      }
  
      @Autowired
      protected void setDAO( D dao )
      {
          this.dao = dao;
      }
  
      @Transactional
      public E get( int id )
      {
          return dao.get( id );
      }
  }
  
  @Service
  public class ArticleService extends BaseService&lt;Article, ArticleDAO&gt;
  {
      public ArticleService()
      {
          setDAO( dao );
      }
  }
  
  public abstract class BaseDAO&lt;E&gt;
  {
      public abstract E get( int id );
  }
  
  @Repository
  public class ArticleDAO extends BaseDAO&lt;Article&gt;
  {
      @Autowired
      private SessionFactory sessionFactory;
  
      @Override
      public Article get( int id )
      {
          return ( Article ) sessionFactory.getCurrentSession().load( Article.class, id );
      }
  }
  </code></pre>
  
  <p>In my Controller now, I'm using this to get a specific article:</p>
  
  <pre><code>@RequestMapping( "/{id}/{title}.html" )
  public String article( @PathVariable( "id" ) Integer id, Map&lt;String, Object&gt; map )
  {
      map.put( "article", articleService.get( id ) );
      return "article";
  }
  </code></pre>
  
  <p>Which I'm using in my JSP just like this:</p>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt;
  &lt;%@ taglib prefix="slg" uri="http://github.com/slugify" %&gt;
  &lt;article&gt;
      &lt;c:url value="/blog/${article.id}/${slg:slugify(article.title)}.html" var="articleUrl" /&gt;
      &lt;h2&gt;&lt;a href="${articleUrl}"&gt;${article.title}&lt;/a&gt;&lt;/h2&gt;
      &lt;span&gt;&lt;fmt:formatDate value="${article.creationDate}" pattern="E, dd MMM yyyy" /&gt;&lt;/span&gt;
      &lt;p&gt;
          ${article.text}
      &lt;/p&gt;
  &lt;/article&gt;
  </code></pre>
  
  <p>Here's my hibernate configuration as well:</p>
  
  <pre><code># Properties file with Hibernate Settings.
  
  #-------------------------------------------------------------------------------
  # Common Settings
  
  hibernate.generate_statistics=false
  #hibernate.hbm2ddl.auto=update
  hibernate.show_sql=false
  
  #-------------------------------------------------------------------------------
  # DB specific Settings
  
  # Property that determines which Hibernate dialect to use
  hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  </code></pre>
  
  <p>What I'm doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Debugging <code>sessionFactory.getCurrentSession()</code> results in:</p>
  
  <pre><code>DEBUG : com.example.model.ArticleDAO - SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])
  </code></pre>
  
  <p>I'm adding some global variables, maybe this causing the errors? In my controller there's a method:</p>
  
  <pre><code>@ModelAttribute
  public void addGlobalObjects( Map&lt;String, Object&gt; map )
  {
      map.put( "section", "blog" );
  
      SortedMap&lt;Category, Integer&gt; categories = new TreeMap&lt;Category, Integer&gt;();
      for ( Category category : categoryService.list() )
      {
          categories.put( category, articleService.size( category ) );
      }
  
      Calendar cal = Calendar.getInstance();
      cal.set( Calendar.DAY_OF_MONTH, 1 );
  
      cal.add( Calendar.MONTH, ARCHIVE_MONTHS * -1 );
  
      SortedMap&lt;Date, Integer&gt; archive = new TreeMap&lt;Date, Integer&gt;();
      for ( int i = 0; i &lt; ARCHIVE_MONTHS; ++i )
      {
          cal.add( Calendar.MONTH, 1 );
          archive.put( cal.getTime(), articleService.size( cal ) );
      }
  
      SortedMap&lt;Tag, Integer&gt; tags = new TreeMap&lt;Tag, Integer&gt;();
      for ( Tag tag : tagService.list() )
      {
          tags.put( tag, articleService.size( tag ) );
      }
  
      map.put( "categories", categories );
      map.put( "archive", archive );
      map.put( "tags", tags );
  
      map.put( "categoriesSize", categoryService.size() );
      map.put( "tagsSize", tagService.size() );
  
      map.put( "date", new Date() );
  }
  </code></pre>
  
  <p>For updated article entity see above.</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Eager fetching didn't solved the problem - still the same exception:</p>
  
  <pre><code>@ManyToMany( fetch = FetchType.EAGER, cascade = { CascadeType.ALL } )
  </code></pre>
  
  <p>And finally I'm getting duplicated articles what I don't need...</p>
  
  <p><strong>UPDATE3</strong></p>
  
  <p>Trying <code>Hibernate.initialize()</code> I'm getting this exception:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  </code></pre>
  
  <p><strong>UPDATE4</strong></p>
  
  <p>I changed my get method this way:</p>
  
  <pre><code>@Override
  public Article get( int id )
  {
      // return ( Article ) sessionFactory.getCurrentSession().load( Article.class, id );
      return ( Article ) sessionFactory.getCurrentSession().createCriteria( Article.class ).add( Restrictions.eq( "id", id ) ).uniqueResult();
  }
  </code></pre>
  
  <p>This is NOT a solution, but as I can't handle this problem I'll use this temporarily.</p>
  
  <p>I already tried this (as mentioned <a href="https://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy?answertab=votes#tab-top">here</a>) without success (it changed the ELException from <code>Error reading 'id' on type ...</code> to <code>Error reading 'title' on type ...</code> - maybe i used it wrong?):</p>
  
  <pre><code>@Override
  public Article get( int id )
  {
      Article ret = ( Article ) sessionFactory.getCurrentSession().load( Article.class, id );
      sessionFactory.getCurrentSession().update( ret );
      return ret;
  }
  </code></pre>
  
  <p>A solution is still needed!</p>