Well, let me back up. I want to be able to access lazily initialized collections.
Here is what I have :
A hibernate DAO class:
Code:
public class StudentDAOhbm extends HibernateDaoSupport implements StudentDAO{
/** Creates a new instance of StudentDAOhbm */
public StudentDAOhbm() {
}
public Student loadStudent(int id) {
return (Student) getHibernateTemplate().load(Student.class, new Integer(id));
}
}
The pertinent xml configuration is as follows (from both spring-servlet.xml and applicationContext.xml):
Code:
<bean id="studentManagerTarget" class="somecompany.DAO.hbm.StudentDAOhbm">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="studentManDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="studentManagerTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="studentDetailController" class="somecompany.web.StudentDetailController">
<property name="studentMan">
<ref bean="studentManDao"/>
</property>
</bean>
A controller class as follows:
Code:
public class StudentDetailController implements Controller {
private StudentDAO studentMan;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int studentId = RequestUtils.getIntParameter(request, "studentId", 0)
Student student = studentMan.loadStudent(studentId);
Set names = student.getNames();
Map myModel = new HashMap();
myModel.put("student",student);
return new ModelAndView("studentDetailView", "model", myModel);
}
Now finally the Student class
Code:
public class Student extends Person implements Serializable {
// many other attributes
private Set names;
// other constructors
/** default constructor */
public Student() {
}
public Set getNames() {
return this.names;
}
public void setNames(Set names) {
this.names = names;
}
}
Now when I run this I get an error as follows:
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
caused by the following line in the controller class :
Set names = student.getNames();
I realize that the method getNames is accessing a collection that is lazily initialized. How do I access a lazily initialized set such as the set in the example above? Is there a good example somewhere? I guess I would like some direction. Does anyone have a good idea that I can follow? Again, any help at all is greatly appreciated.