Hi I have two classes named college and student with parent child relationship . the mapping file is as fallows.
<hibernate-mapping> <class name="com.sample.College" table="College"> <id name="dbid" column="College_id" type="long"> <generator class="increment" /> </id> <list name="studentList" table="College_Student" lazy="true" > <key column="dbid"/> <index column="rmxcamp_number"/> <many-to-many class="com.sample.Student" column="college_studid"/> </list> </class> </hibernate-mapping>
and code to CREATE, ADD and GETting records are as fallows
CREATE: in a HTTP Request - Response model ex: public long createCollege(String name,String city){ Session hibernatesession =hibernateSessionFactory.getCurrentSession(); Transaction ts=hibernatesession.beginTransaction(); College c1=new College(name,city); // here empty student list is created hibernatesession.save(c1); ts.commit(); httpsession.setAttribute(c1.getDbid(),c1); return c1.getDbid(); } here c1 is placed in the HTTP session with dbid, to get that object in subsequent request
ADD: in a HTTP Request - Response model
public void addStudent(long dbid, String stundentname, float marks){ College c1=(College)httpsession.getAttribute(dbid()); Session hibernatesession =hibernateSessionFactory.getCurrentSession(); Transaction ts=hibernatesession.beginTransaction(); Student s1=new Student(studentname,marks); c1.addStudent(s1); // s1 is added to list of student; hibernatesession.save(s1); hibernatesession.update(c1); ts.commit(); }
GETting objects : in a HTTP Request - Response model
public List<Student> getStudents(long dbid){ List<Student> studlist=null; College c1=(College)httpsession.getAttribute(dbid()); Session hibernatesession =hibernateSessionFactory.getCurrentSession(); Transaction ts=hibernatesession.beginTransaction(); studlist=c1.getStudentList(); // displaying the list values giving LazyInitialation exception here. ts.commit(); }
the exception is as fallows"
98430 [btpool0-0] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.sample.College.studentList, no session or session was closed org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.sample.College.studentList, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) at org.hibernate.collection.PersistentList.toString(PersistentList.java:395) at com.sample.College.getStudentList(UserServiceUtil.java:600)
I think, the college object is detached from the hibernate session,it is attached to the previous hibernate session but it is not working even i merged it to the new hibernate session as fallows or with get() method. ex: c1= (College)hibernatesession.merge(c1); or c1 =(College)hibernateSession.get(College.class,c1.getDbid());
it is giving the same above exception.
Please Help me on this. How to lazy load a collection in a request -response model
thanks ramanaiah a v
|