I think filters were designed for this kind of  job
Have you seen this?
http://stackoverflow.com/questions/4988397/gwt-requestfactory-how-to-use-single-entitymanager-per-request
<http://stackoverflow.com/questions/4988397/gwt-requestfactory-how-to-use-single-entitymanager-per-request>

On Sun, Mar 27, 2011 at 7:27 PM, rwiatr <[email protected]> wrote:

> 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 <[email protected]> wrote:
> > Try: Hibernate.initialize(hList);
> >
> > On Mar 27, 11:47 am, rwiatr <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

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

Reply via email to