Results 1 to 3 of 3

Thread: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

  1. #1
    Join Date
    May 2012
    Posts
    3

    Angry org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    Hi guys i'm having a weird problem in here, i'm using JPA, Hibernate and Spring MVC
    well in the controller class all the methods work great when i test them in the web browser EXCEPT the :
    public String getModuleFormation(long id) method that returns one object and it gives me the following error :

    org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    as a root cause, but yesterday i tried it worked without problem in the localhost:45045/GestionModules/detail/xx URL
    using the EL.
    please what would cause this problem in my code (i know it's something about lazy loading i read a lot about it but i need to change the whole code if i want it to work again and what's the solution that i can only add to the function, thanks

    detail.jsp :
    Code:
    	<c:if test="${!empty detailModule}">
    	
    	${detailModule.idModule}
    	${detailModule.libModule}
    	</c:if>
    POJO Class + JPA :
    Code:
        @Entity
        @Table(name="ModuleFormation")
        public class ModuleFormation {
    	
    	private long idModule;
    	private String libModule;
    	
    	public ModuleFormation() {
    		// TODO Auto-generated constructor stub
    	}
    	
    	public ModuleFormation(String libModule) {
    		this.libModule = libModule;
    	}
    	
    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seqModule")
    	@SequenceGenerator(name="seqModule", sequenceName = "seqModuleFormation")
    	@Column(name="idModule")
    	public long getIdModule() {
    		return this.idModule;
    	}
    	
    	public void setIdModule(long idModule) {
    		this.idModule = idModule;
    	}
    	
    	@Column(name="libModule", nullable=false, length = 100)
    	public String getLibModule() {
    		return this.libModule;
    	}
    	
    	public void setLibModule(String libModule) {
    		this.libModule = libModule;
    	}
    	
        }
    DAO Class :
    Code:
        @Repository
        public class ModuleFormationDAOImpl implements ModuleFormationDAO {
    
    	@Autowired
    	private SessionFactory sessionFactory;
    	
    	
    	public void ajouterModuleFormation(ModuleFormation module) {
    		sessionFactory.getCurrentSession().save(module);
    	}
    
    	public void supprimerModuleFormation(long idModule) {
    		ModuleFormation module = (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
    		if(module != null)
    			sessionFactory.getCurrentSession().delete(module);
    	}
    	
    	public List<ModuleFormation> listModuleFormation() {
    		
    		return sessionFactory.getCurrentSession().createQuery("from ModuleFormation")
    				.list();
    		
    	}
    	
    	public ModuleFormation getModuleFormation(long idModule) {
    		 return (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
    	}
    
    	public void majModuleFormation(ModuleFormation module) {
    		sessionFactory.getCurrentSession().merge(module);
    	}
    	
        }
    Service Class :
    Code:
        @Service
        public class ModuleFormationServiceImpl implements ModuleFormationService {
    
    	@Autowired
    	private ModuleFormationDAO moduleDao;
    	
    	@Transactional
    	public void ajouterModuleFormation(ModuleFormation module) {
    		moduleDao.ajouterModuleFormation(module);
    	}
    	
    	@Transactional
    	public void supprimerModuleFormation(long idModule) {
    		moduleDao.supprimerModuleFormation(idModule);
    	}
    	
    	@Transactional
    	public List<ModuleFormation> listModuleFormation() {
    		return moduleDao.listModuleFormation();
    	}
    	
    	@Transactional
    	public ModuleFormation getModuleFormation(long idModule) {
    		return moduleDao.getModuleFormation(idModule);
    	}
    	
    	@Transactional
    	public void majModuleFormation(ModuleFormation module) {
    		moduleDao.majModuleFormation(module);
    	}
        }
    Controller Class :
    Code:
        @Controller
        public class ModuleFormationController {
    	
    	@Autowired
    	private ModuleFormationService moduleService;
    	
    	@RequestMapping("/module")
    	public String listModulesFormations(Map<String, Object> map) {
    		
    		map.put("module", new ModuleFormation());
    		map.put("moduleList", moduleService.listModuleFormation());
    		
    		return "module";
    	}
    	
    	@RequestMapping(value = "/ajouter", method = RequestMethod.POST )
    	public String ajouterModuleFormation(@ModelAttribute("module")
    	ModuleFormation module,BindingResult result) {
    		
    		moduleService.ajouterModuleFormation(module);
    		
    		return "redirect:/module";
    	}
    	
    	
    	@RequestMapping(value = "/supprimer/{idModule}")
    	public String supprimerModuleFormation(@PathVariable("idModule")
    	long idModule) {
    		moduleService.supprimerModuleFormation(idModule);
    		
    		return "redirect:/module";
    	}
    	
    
    	@RequestMapping(value= "/detail/{idModule}")
    	public String getModuleFormation(@PathVariable("idModule")
    	long idModule,Map<String, Object> map) {
    		map.put("detailModule", moduleService.getModuleFormation(idModule));
    		return "/detail";
    	}
    	
    		
    	@RequestMapping(value= "/detail/modifier", method = RequestMethod.POST )
    	public String majModuleFormation(@ModelAttribute("detailModule")
    	ModuleFormation module, BindingResult result) {
    		moduleService.majModuleFormation(module);
    		return "detail/{idModule}";
    	}
    
        }

  2. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    With just what you posted, I don't see any associations to be lazy loaded. If you query for one object type then all the simple properties of that object will have data. Those top level objects won't be proxied and lazy loaded. Only associated objects would. So I think there is something missing that you didn't post. What does your entire jsp page look like? Also you should post your entire stacktrace. Which object is actually throwing the LazyInitializationException.

    Thanks

    Mark

  3. #3
    Join Date
    May 2012
    Posts
    3

    Default

    Quote Originally Posted by bytor99999 View Post
    With just what you posted, I don't see any associations to be lazy loaded. If you query for one object type then all the simple properties of that object will have data. Those top level objects won't be proxied and lazy loaded. Only associated objects would. So I think there is something missing that you didn't post. What does your entire jsp page look like? Also you should post your entire stacktrace. Which object is actually throwing the LazyInitializationException.

    Thanks

    Mark
    thanks Mark !
    i found the solution on http://stackoverflow.com/questions/1...alize-proxy-no

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •