This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

I probably shouldnt have @Transactional defined above but I was just playing around with that, even without it I still get the same results.
As soon as I try to get Topics, it gives me this exception:

Code:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.logixplayer.project1.domain.Category.topics, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)

Yeap you guessed it, the root cause?
"no session or session was closed"

I realize that the session was closed the second I got a list of categories...I understand that...but how does the session stay alive again or come back if I want to lazyload?

Still no luck, OpenEntityManagerInViewFilter LazyInitializationException HELP!!

Hi people,

I am still battling this LazyInitializationException using JPA+Spring+Hibernate.

I have followed suggestions such as
1) move the filter definition for OpenEntityManagerInViewFilter to the top of the web.xml file so to be the first filter to intercept calls
2)Observe log files to see if the OpenEntityManagerInViewFilter is firing. It looks like it is:

I am missing something but I do not know what!
I know why I am getting the exception as I stated in my earlier post. The session is being closed when I try to access children (topics) of each category (parent). This is why I was trying to use OpenEntityManagerInViewFilter in the first place.
My only hunch is that the sessions are not unique but distinct. I.e. the session that is being used to get Categories is not being kept open for some reason.

The only thing that jumps out at me is the class definition for OpenEntityManagerInViewFilter:

Code:

Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

Servlet 2.3???
I am using Servlet 2.5 and JSP 2.1.
Does OpenEntityManagerInViewFilter not work for servlet 2.5 specs??

I am totally stumped

Anyone can lend some help? Please do...I have been stuck for a very long time
Thanks so much!!

Comment

the point of this exercise is to actually not take a performance hit.There will be potentially thousands of users at one single time, I cannot afford to load EAGER. because then each user will have the same categories and the same topics in memory.

I need to figure out how to load lazy.

I have searched thoroughly on the net but cannot find a solution to this problem, even though it is quite a common issue.

I thought the OpenEntityManagerInViewFilter needed to be simply plugged in...

I really need some help on this one...please reach out...

thanks again

Comment

Ok, so we are dealing with "Detachment". This can happen in many ways i.e. when an entity is serialized,
when a rollback occurs etc.. In your case I believe it is because @Transactional for findByName method has finished.

One solution to this is to trigger Lazy Loading: (this would be done in the findByName method as it is annotated with @Transaction)

Here you need to invoke a method on the getTopics as Lazy Loading relationship may be a proxy that will not load
the entity. We have to call a method (t.callMethod()) on the entity to ensure that it is really pulled from the database.

Another way is to change the way you are using Transactions, currently the entity manager created by the @Transaction
only remains open as long as the transaction in which it was created has not finished. So you need to wrap the transaction around the class
using the findByName ensuring any calls made to detached entities are made within this method. So an example would be:

In other words you need to keep the persistence context open for the duration for the processing of the lazy loaded relationship to be resolved. I hope this helps in explaining the issue you are having.

Note: I used the myEclipse reverse generate tool to create my DAO's and domain objects.

Now, moving on to my controller. First of all, the reason why I tried to use annotations was because of its elegance. I really don't want to use programmatic style transaction demarcations (and I am sure there is a solution to this using annotations)

In the controller below, just like the issue I was getting when I tried to lazily get Topics, in this case I am trying to get more categories of the parent category BUT I get a LazyLoadingException.

Comment

I thought I communicated that, I am using OpenEntityManagerInViewFilter but its not working, which is why I am getting this lazyloadingexception.
Can someone post a working example or just tell me what's going wrong here??
Appreciate it, thank you.

Comment

maybe I am wrong but I believe your problem is because you have your code inside CategoryController, if "public ModelAndView onSubmit(Object command)" is not a transactional method, then all calling methods inside are REQUIRED, so, each method is independent in each transaction, so each method close itself transaction when it ends.

I would recomend to make other method including all problematic code and then pass this method to controller.

Comment

maybe I am wrong but I believe your problem is because you have your code inside CategoryController, if "public ModelAndView onSubmit(Object command)" is not a transactional method, then all calling methods inside are REQUIRED, so, each method is independent in each transaction, so each method close itself transaction when it ends.

I would recomend to make other method including all problematic code and then pass this method to controller.

IMHO.

Perhaps what you are trying to say is that there should only be one overall transaction in the controller?

Regardless, even if there was one transaction in the controller, my problem still stands, observe:

Let's just say all I had was the above in my controller in "some other method".
The point is that the session from

Code:

categoryService.findByName("Autos", null);

is closed right after this call (to findByName)
Scenario: I got my OpenEntityManagerInViewFilter hooked up.
Question: How do I re-attach to the same session just used in categoryService.findByName("Autos", null); so I avoid the LAZY LOAD EXCEPTION when I do:

Code:

Set<Category> subCategories = category.getCategories();

Common guys, what am I missing :P
I thought OpenEntityManagerInViewFilter was as simple as declaring it in web.xml. I got my services annotated with @Transactional YET there is something that is mis-configured, not configured or just missing.
I am not sure to say Help?? or Help!!
Gosh this is the best one yet...

class DAO{
propagation=Propagation.REQUIRED
method TOCONTROLLER{
method A (propagation=Propagation.REQUIRED)
method B (propagation=Propagation.REQUIRED)
method C (parameter mehotd A, parameter method B)(propagation=Propagation.REQUIRED)
//here you will not have exception because parent's method is transactional and required, so this is a transaction over other transactions included in sub_methods
}
}

Essentially this exception is happening outside of the first transaction (to get categories). I thought that by using OpenEntityManagerInViewFilter it can somehow re-open/re-attach that original session ....

Comment

now I see other think, maybe you will have same problem with other code like this, because you are doing the same.

Code:

List<Topic> topics = topicService.findByCategoryID(subCategory.getCategoryId(), null);
System.out.println("****now to get topics if there are any!****");
for (Topic topic : topics) {
System.out.println("topic: "+topic.getTitle());
System.out.println("****now to get submissions if there are any!****");
Set<Submission> submissions = topic.getSubmissions();

Comment

Appreciate the reply but that's really not the way I want to go.
The solution you posted is really not very flexible and it totally defeats the purpose of LazyLoading.

I would like to lazy load when I need to, which is why there is a OpenEntityManagerInViewFilter in the first place.

Note the following definition of OpenEntityManagerInViewFilter:

Code:

Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

The definition is perfect. It allows lazy loading in web views despite the original transactions already being completed.
But it's still not working. Some configuration missing somewhere!!!