开发者

How to pass (properly) a Java Object from a Servlet to a JSP page

I've been everywhere on the web, and i'm little lost and couldn't solve my problem.

I'm creating a web application using Eclipse, with JSP on the client side, and Servet/Hibernate on the server side. I'm trying to pass an Object to a JSP page form a servlet.

In the servlet :

Contact c = dao.getContact(dataID);

request.setAttribute("data", c);

getServletContext()
    .getRequestDispatcher("/"+url+"?id="+dataID).forward(request, response);

In the JSP page :

Contact contact = (Contact)request.getAttribute("data");

Contact is an ORM, which has attributes like "lastname".

But i'm getting this error message :

GRAVE: "Servlet.service()" pour la servlet GetData a généré une exception
org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
    at domain.DAOContact.getContact(DAOContact.java:39)
    at domain.GetData.doGet(GetData.java:27)

So I tried to figure out why, and somewhere I read that I need to set the current session context to "thread" in the hibernate config file. But when I did this, i get this message :

GRAVE: Servlet.service() for servlet [GetData] in context with path [/CarnetContacts] threw exception [org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at domain.Contact_$$_javassist_1.getLastName(Contact_$$_javassist_1.java)

The second line of the message indicates it has to do with lazy loading, but the last line i shows that the bug occurs when the program is trying to get the "lastname" attribute, which is basically a string, so I don't think it's "lazy loaded".

So if somebody could help me find a solution, that would be very kind.

Code of the dao method :

    public Contact getContact(int contactId){
        Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTran开发者_StackOverflowsaction();
        t.begin();
        Contact contact =(Contact) session.load(Contact.class, new Integer(contactId));
        t.commit();
        return contact; 
    }


Your specific problem in this case is that you use Session.load() instead of Session.get().

load() returns a lazily-initialized proxy, therefore it should be used only in special cases, in the most cases you need to use get().

Generally speaking, besides this particular case, problem with lazy initialization in view layer can be solved using one of the following approaches:

  • Use Open Session in View pattern

  • Fetch all required data before rendering the view (using JOIN FETCH, Hibernate.initialize(), etc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜