0

I do not understand why this is happening. Based on the code path, I should be within the same thread and session should be present, when this exception is raised.

Can somebody tell what I am missing here?

I have setup

<property name="hibernate.current_session_context_class">thread</property>

in hibernate.cfg.xml file

I have created the following code in Servlet filter

 try{
  factory.getCurrentSession().beginTransaction();  
  httpRequest.getRequestDispatcher("/public/index.html").forward(httpRequest,response);
 }finally{
  factory.getCurrentSession().getTransaction().commit();  
 }

In index.xhtml file, I have the following call:


do some stuff

index.xhtml is using template that uses ui include to load a menu.xhtml file. the menu file then inserts the menulist. servicesMenuItems call from menuItemsViewController eventually ends up in the following code

 public Collection<Bulletin> getBulletin(User bean){
   Session session=factory.getCurrentSession();
   try{
     session.refresh(bean);
     if(bean.getObligations().size()>0){
     do some stuff
   }

As you can see session is present, when session.refresh(bean) is not throwing exception but bean.getObligations() is throwing,

failed to lazily initialize a collection of role: data.User.obligations, could not initialize proxy - no Session"

Any ideas why?

Tiny
  • 24,933
  • 92
  • 299
  • 571
Masoud
  • 63
  • 7
  • possible duplicate of [Hibernate: org.hibernate.LazyInitializationException: could not initialize proxy - no Session](http://stackoverflow.com/questions/14542140/hibernate-org-hibernate-lazyinitializationexception-could-not-initialize-proxy) – Viraj Nalawade Apr 24 '15 at 05:49
  • possible duplicate of http://stackoverflow.com/questions/21574236/org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-sess – M.J. Apr 24 '15 at 06:29
  • That exception occurs, when a lazily initialized object is attempt outside the transaction boundary. You need to precisely initialize lazily initialized objects in order to use them outside the transaction boundary. One of the ways to get rid of the exception is to use fetch joins. You can also abusively use the eager fetch type which I never even think of using. JSF on the other hand, has got absolutely nothing to do with this problem. – Tiny Apr 24 '15 at 10:24

2 Answers2

0

Default lazy property is true. That mean hibernate will not fill a object property on a entity. In that case, Hibernate will not fill Obligation in User bean. When you try get obligation, a exception will be throw.

Hibernate Eager vs Lazy Fetch Type

You can change lazy property or must fill Obligation by yourselt.

luanvu
  • 79
  • 7
0

Ok.. I fixed the issue but still i do not know what was causing it... Upon close examination of my code, i noticed that i am putting user in session.. and on first attempt, everything was working fine and on second attempt to loaf the page, exception was issued...

I started to clear the session and put the user in session every single time i try to access the page.. now i do not see the exceptions any more...

Masoud
  • 63
  • 7