Announcement Announcement Module
Collapse
No announcement yet.
[Hibernate] Failed to lazily initialize a collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Hibernate] Failed to lazily initialize a collection

    Hello,

    I'm curruntly building a webapp built on the model of the petclinic sample app.

    I am facing an error of initialization of a collection when executing a SimpleFormController. Hibernate says it doesn't find the session. I use HibernateTemplate to manage data access.

    Here is the error :
    Code:
    net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209) at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71) at net.sf.hibernate.collection.Set.toString(Set.java:217) at java.lang.String.valueOf(Unknown Source) at java.lang.StringBuffer.append(Unknown Source) at com.spm.bulletin.Entity.toString(Entity.java:66) at java.lang.String.valueOf(Unknown Source) at java.util.AbstractCollection.toString(Unknown Source) at java.lang.String.valueOf(Unknown Source) at java.lang.StringBuffer.append(Unknown Source) at com.spm.bulletin.web.AddPupilsClassForm.onSubmit(AddPupilsClassForm.java:44) at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248) at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:243) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:675) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:623) at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:384) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:353) at javax.servlet.http.HttpServlet.service(HttpServlet.java:716) at javax.servlet.http.HttpServlet.service(HttpServlet.java:809) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200) at
    Here is the XML declaration of the addClassForm bean :
    Code:
    	<bean id="addClassForm" class="com.spm.bulletin.web.AddPupilsClassForm">
    		<property name="formView"><value>classForm</value></property>
    		<property name="successView"><value>classRedirect</value></property>
    		<property name="validator"><ref local="classValidator"/></property>
    		<property name="school"><ref bean="school"/></property>
    	</bean>
    The Java class extending SimpleFormController :
    Code:
    public class AddPupilsClassForm extends AbstractSchoolForm &#123;
    
    	public AddPupilsClassForm&#40;&#41; &#123;
    		// OK to start with a blank command object
    		setCommandClass&#40;PupilsClass.class&#41;;
    		// activate session form mode to allow for detection of duplicate submissions
    		setSessionForm&#40;true&#41;;
    	&#125;
    
    	protected Map referenceData&#40;HttpServletRequest request&#41; throws ServletException &#123;
    		Map refData = new HashMap&#40;&#41;;
    		refData.put&#40;"teachers", getSchool&#40;&#41;.getTeachers&#40;&#41;&#41;;
    		return refData;
    	&#125;
    	
    	/** Method inserts a new <code>PupilsClass</code>. */
    	protected ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException arg3&#41; throws ServletException &#123;
    		PupilsClass pupilsClass = &#40;PupilsClass&#41; command;
    		int mainTeacherId = Integer.parseInt&#40;request.getParameter&#40;"mainTeacherId"&#41;&#41;;
    		System.out.println&#40;"mainTeacherId &#58; "+mainTeacherId&#41;;
    		if &#40;mainTeacherId != -1&#41; &#123;
    			System.out.println&#40;"teachers &#58; " + getSchool&#40;&#41;.getTeachers&#40;&#41;&#41;;
    			Teacher teacher = &#40;Teacher&#41;Entity.getById&#40;getSchool&#40;&#41;.getTeachers&#40;&#41;, Teacher.class, mainTeacherId&#41;;
    			//System.out.println&#40;"pupilsClass &#58; "+pupilsClass&#41;;
    			pupilsClass.setMainTeacher&#40;teacher&#41;;
    		&#125;
    		System.out.println&#40;"pupilsClass &#58; " + pupilsClass&#41;;
    		//pupilsClass.setMainTeacher&#40;getSchool&#40;&#41;.loadTeacher&#40;pupilsClass.getMainTeacher&#40;&#41;.getId&#40;&#41;&#41;&#41;;
    		// delegate the insert to the Business layer
    		getSchool&#40;&#41;.storePupilsClass&#40;pupilsClass&#41;;
    		return new ModelAndView&#40;getSuccessView&#40;&#41;, "pupilsClassId", Integer.toString&#40;pupilsClass.getId&#40;&#41;&#41;&#41;;
    	&#125;
    
    	protected ModelAndView handleInvalidSubmit&#40;HttpServletRequest request, HttpServletResponse response&#41;
    			throws Exception &#123;
    		return disallowDuplicateFormSubmission&#40;request, response&#41;;
    	&#125;
    
    &#125;
    The error is thrown when calling the getSchool().getTeachers() method in the onSubmit(...).

    And the class managing data access :
    Code:
    public class HibernateSchool extends HibernateDaoSupport implements School &#123;
    
    	public Collection getTeachers&#40;&#41; throws DataAccessException &#123;
    		return getHibernateTemplate&#40;&#41;.find&#40;"from Teacher teacher order by teacher.lastName, teacher.firstName"&#41;;
    	&#125;
    &#91;...&#93;

  • #2
    This is more of a Data Access problem. Your objects are lazy-init and I assume you are not using OpenSessionInViewFilter/Interceptor.
    I am not familiar with the PetClinit application on Spring but a quick fix would be disable the lazy functionality of your objects - ofc, this thing has it's consequences.

    Comment

    Working...
    X