Pages

Sunday, April 19, 2020

ERROR : DefaultDispatcherErrorHandler Exception occurred during processing request: failed to lazily initialize a collection of role,could not initialize proxy - no Session

ERROR : DefaultDispatcherErrorHandler Exception occurred during processing request: failed to lazily initialize a collection of role: com.javaimplant.socialnetwork.model.User.friend, could not initialize proxy - no Session
 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javaimplant.socialnetwork.model.User.friend, could not initialize proxy - no Session


I got this error while trying to map a One to Many relation in my Hibernate Entity.
The issue can be resolved by adding the following fetch property above our Entity Property.

 @OneToMany(fetch = FetchType.EAGER)  
      private Set<User> friend;  



Thursday, January 9, 2020

Autowire same bean using different Scopes in Class

Definitions in Application Context
      <bean id="RequestHit" class="com.springimplant.mvc.HitCounter" scope="request">  
           <aop:scoped-proxy/>  
      </bean>  
      <bean id="applicationHit" class="com.springimplant.mvc.HitCounter" scope="application">  
           <aop:scoped-proxy/>  
      </bean>  
      <bean id="sessionHit" class="com.springimplant.mvc.HitCounter" scope="session">  
           <aop:scoped-proxy/>  
      </bean>  
      <mvc:interceptors>  
           <mvc:interceptor>  
                <mvc:mapping path="/project/**"/>  
                <bean class="com.springimplant.mvc.interceptors.GlobalInterceptor"></bean>  
           </mvc:interceptor>  
      </mvc:interceptors>  

Defination in Class
 package com.springimplant.mvc.interceptors;  
 import java.util.Date;  
 import javax.annotation.Resource;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;  
 import com.springimplant.mvc.HitCounter;  
 public class GlobalInterceptor extends HandlerInterceptorAdapter {  
      @Resource   
      private HitCounter RequestHit;  
      @Resource  
      private HitCounter applicationHit;  
      @Resource  
      private HitCounter sessionHit;  
      @Override  
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
                throws Exception {  
           RequestHit.setHits(RequestHit.getHits()+1);  
           System.out.println("Request Hits "+RequestHit.getHits());  
           applicationHit.setHits(applicationHit.getHits()+1);  
           System.out.println("Application Hits "+applicationHit.getHits());  
           sessionHit.setHits(sessionHit.getHits()+1);  
           System.out.println("Session Hits "+sessionHit.getHits());  
           request.setAttribute("currentDate",new Date());  
           return super.preHandle(request, response, handler);  
      }  
 }  

Wednesday, January 8, 2020

Autocomplete not working in spring bean configuration file

Project Properties > Spring > Bean Support > Config Sets > Add New Group (contains all configuration files). Then open applicationContext.xml again.