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?

3
  • possible duplicate of Hibernate: org.hibernate.LazyInitializationException: could not initialize proxy - no Session Commented Apr 24, 2015 at 5:49
  • possible duplicate of stackoverflow.com/questions/21574236/… Commented Apr 24, 2015 at 6: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. Commented Apr 24, 2015 at 10:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.