-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: really strange when use webmacro and hibernate to write ...
PostPosted: Thu Sep 04, 2003 5:14 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
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;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 5:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
NOthing should be trying to invoke elements()!


That is a private Hibernate implementation detail! It is not intended to be exposed to applications.


Top
 Profile  
 
 Post subject: U mean it's webmacro's problem ?
PostPosted: Thu Sep 04, 2003 5:21 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
I don't know why such method were invoked ?
Do you think it's webmacro's problem which also use java reflection ?

(webmacro is much like velocity)


Top
 Profile  
 
 Post subject: urgent ! anybody can help ?
PostPosted: Thu Sep 04, 2003 10:47 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
gavin means it's WebMacro's problem., but how to overcome it ? Anyone can help ?

Thanks a lot !


Top
 Profile  
 
 Post subject: Urgent ! pls help !!!
PostPosted: Sun Sep 28, 2003 5:22 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
Why the following error doesn't occur when iterating over the persistent collection before put it into Velocity context for rendering ?

It's the biggest problem for our project now. Anyone can help ?



Quote:
<table><!-- WARNING: #foreach: $employees: 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\TestView.htm:12.2 --> </table>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2003 5:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
What happens if you use 2.1? 2.1 has no elements() method.


(Probably it will just call entries(), but lets find out)


Anyway, I don't understand why this method is being called at all!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2003 5:46 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
It works with Hibernate 2.1, but Webmacro use reflection to determine what to be called, so why such method can not be called and why it throws NullPointerException when being called ?

By the way, do you think 2.1 is robust enough to be put into production ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2003 5:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
By the way, do you think 2.1 is robust enough to be put into production ?


No.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.