I encounter a really strange situation when use hibernate and webmacro to write a servlet. Everything works when I uncomment the commented code block. But when I comment them out, information returned in the HTML page says :
</tr><!-- WARNING: #foreach: $requirements: Attempt to invoke method public java.util.Iterator net.sf.hibernate.collection.Set.elements() on object net.sf.hibernate.collection.Set raised an exception: java.lang.NullPointerException
java.lang.NullPointerException at C:\resin-ee-2.1.10\webapps\hr\WEB-INF\classes\TestRequirementView.htm:51.2 --></table>
I use "lazy" for all the hibernate collections. Seems hibernate loads and keeps all the objects into session when I iterate over them first. But how can I avoid the commented blocks ?
thx !
The following is the source code for this servlet:
Code:
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletException;
import hibernate.*;
import test.WalkPathParser;
import test.DataStore;
import treeview.Tree;
import treeview.TreeNode;
import treeview.TreeNodeInfo;
import org.webmacro.servlet.WMServlet;
import org.webmacro.servlet.WebContext;
import org.webmacro.servlet.HandlerException;
import org.webmacro.Template;
import org.webmacro.ResourceException;
import org.webmacro.PropertyException;
public class TestRequirementManager extends WMServlet {
//sf is thread-safe
private SessionFactory sf;
public void start() throws ServletException {
try {
sf = DataStore.getHibernateSessionFactory();
} catch (HibernateException e) {
throw new ServletException(e.getMessage());
}
super.start();
}
public Template handle(WebContext context) throws HandlerException {
String wp = null;
String wpath = "";
HttpSession sess = null;
Session session = null;
Tree tree = null;
// enable Chinese support
context.getResponse().setContentType("text/html; charset=gb2312");
try {
context.getRequest().setCharacterEncoding("GB2312");
} catch (UnsupportedEncodingException e) {
throw new HandlerException(e.getMessage());
}
// variable preparation
sess = context.getRequest().getSession();
//OUCreate should be used with OUTree, so we get the created tree first
tree = (Tree) sess.getAttribute("catalogTree");
wp = context.getRequest().getParameter("wp");
WalkPathParser wpp = null;
wpp = new WalkPathParser(wp);
int[] walkpath = wpp.returnWalkPath();
Test test = null;
try {
// datastore preparation
session = sf.openSession();
String formName = null;
formName = context.getForm("formName");
// user input validation
if (formName == null) {
if ((test = (Test) sess.getAttribute("currentTest")) == null) {
// load a test object for test purpose
test = (Test) session.load(Test.class, new Long(1));
}
Query q = session.createQuery("from hibernate.Test as test");
List tests = q.list();
Set requirements = test.getRequirements();
/* when i uncomment this block , things work
for (Iterator iterator = requirements.iterator(); iterator.hasNext();) {
TestRequirement r = (TestRequirement) iterator.next();
}
*/
try {
context.put("test", test);
context.put("tests", tests);
context.put("requirements", requirements);
writeTemplate("TestRequirementView.htm",context.getResponse().getOutputStream(), context);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ResourceException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (PropertyException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
return null;
}
if (formName.equals("switchTest")) {
String testIdStr = context.getForm("testId");
int testId = Integer.parseInt(testIdStr);
test = (Test) session.load(Test.class, new Long(testId));
sess.setAttribute("currentTest", test);
Query q = session.createQuery("from hibernate.Test as test");
List tests = q.list();
Set requirements = test.getRequirements();
/* when i uncomment this block , things work
for (Iterator iterator = requirements.iterator(); iterator.hasNext();) {
TestRequirement r = (TestRequirement) iterator.next();
}
*/
try {
context.put("test", test);
context.put("tests", tests);
context.put("requirements", requirements);
writeTemplate("TestRequirementView.htm",context.getResponse().getOutputStream(), context);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ResourceException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (PropertyException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
return null;
}
} catch (HibernateException e) {
throw new HandlerException("hibernate exception caught!");
} finally {
try {
session.close();
} catch (HibernateException e) {
// such exception should not occurred
throw new HandlerException(e.getMessage());
}
}
return null;
}
}