Sunday, March 27, 2011

Re: Hibernate session in RequestFactory

Thanks for the response Nooonika.
Unfortunately it doesn't work for me. I'm looking for a mechanism to
open a session before request is processed and close after (or
something like that).
Here's something that works, at least for now.

public class SessionRequestFactoryServlet extends
RequestFactoryServlet {
private static final ThreadLocal<Session> perThreadSession = new
ThreadLocal<Session>();

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException
{

perThreadSession.set(HibernateUtil.getSessionFactory().openSession());
try {
super.doPost(request, response);
} finally {
perThreadSession.get().close();
perThreadSession.set(null);
}
}

public static Session getPerThreadSession() {
return perThreadSession.get();
}
}
in the DAO object:
@Transient
public static List<HierarchyImpl> rootHierarchyRequest() {
try {
Session session = SessionRequestFactoryServlet
.getPerThreadSession();
List<HierarchyImpl> hList = new ArrayList<HierarchyImpl>(session
.createQuery("from Hierarchy where parent is null").list());
return hList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
And a servlet entry in web.xml
<servlet-class>app.server.requestfactory.SessionRequestFactoryServlet</
servlet-class>

RequestFactoryServet uses ThreadLocal variables so I guess it's ok.
Anyone knows a more suitable way to achieve this? I don't have any
experience with such thing and I'm afraid it can cause much more
problems in the future. Anyone has any suggestions?

On 27 Mar, 16:20, Nooonika <nooon...@gmail.com> wrote:
> Try: Hibernate.initialize(hList);
>
> On Mar 27, 11:47 am, rwiatr <rwi...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hello,
> > I have a problem with RequestFactory and Hibernate session.
> > [hibernate.LazyInitializationException] - failed to lazily initialize
> > a collection of role: app.server.dao.HierarchyImpl.children, no
> > session or session was closed
>
> >         @OneToMany(cascade = CascadeType.ALL)
> >         @JoinTable(name = "HIERARCHY_RELATE", joinColumns =
> > { @JoinColumn(name = "parent_id") }, inverseJoinColumns =
> > { @JoinColumn(name = "child_id") })
> >         public Set<HierarchyImpl> getChildren() {
> >                 return children;
> >         }
>
> >         public static List<HierarchyImpl> rootHierarchyRequest() {
> >                 int i = 0;
> >                 try {
> >                         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
> >                         Session session = sessionFactory.openSession();
> >                         List<HierarchyImpl> hList = new ArrayList<HierarchyImpl>(session
> >                                         .createQuery("from Hierarchy where parent is null").list());
> >                         session.close();
> >                         return hList;
> >                 } catch (Exception e) {
> >                         e.printStackTrace();
> >                 }
> >                 return null;
> >         }
> > Adding FetchType.EAGER helps but in long terms will not solve my
> > problem.
> > Is there a good way to manage Hibernate session by RequestFactory?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment