Here is my reply I have already given you, when you posted this same question in the EJB forum, and I moved it here for you.
Why did you delete that thread that had my response?
Exactly. In your Facade is where you get your EntityManager, this is the same as a Session in Hibernate. So you do a query with the EntityManager in the Facade, right now, Hibernate will lazy load your result set, maining there are prozy objects in your List, they only have the IDs, so when you return the List back to the Servlet, you are no longer inside a "transaction/session" with the EntityManager, it is gone. Then you try to access these "proxy" objects that do not have the full information, but it can't get that data because you no longer have the EntityManager.
Off the top of my head, I don't remember the exact syntax to make it eager fetch, since it looks like only one table, but I do know that in your Facade, after you get the ResultList, if you try to access the Collection, it will load the data for you, then you can return it from the Facade.
Your Servlet cannot access the database to initialize your data because you are not in a Hibernate Session.
I think the quick and dirty way is to call size() on the list before you return it. But you must also consider if the items in the list are intialized too. Maybe you need to hold open your session or some different session management.
The clean way is to call Hibernate.initialize() on the list before you return it. This works on persistent objects so I hope it works on lists from queries as well. [ December 29, 2006: Message edited by: Mr. C Lamont Gilbert ]
Sorry for replying to a very old post. I had the exact same issue while using the @ManyToOne mapping in my application. I blindly followed your "quick and dirty" way and it worked just like that. Now, I am eager to know as how it worked and what's the issue previously. I was not able to comprehend the reason from the above posts.
Suppose you have a One to many relation between A and B. When you fetch an instance of A you will be getting a Collection of object B those related to A. Now you might not be needing all the Entities in that collection and hence the persistence context lazily loads the objects, That is it will have a collection proxies of object B not containing any data except the primary key field of B. Now only when you access the collection your Entity manager will load the actual data for you.
By default i believe in JPA it fetches the collection eagarly. Have you specified it anywhere in you application to fetch type as Lazy?
I understand a part of what you said. So, in short by calling the size() method you are actually "waking it" up? I haven't mentioned Lazy fetch anywhere. Infact I have a many-to-many mapping between two of my entities which does the eager fetch properly. I am not sure why this is not getting following the eager fetch. I will look into it.
The knights of nee want a shrubbery. And a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss