my dog learned polymorphism
The moose likes Spring and the fly likes Can't Retrieve EntityManager With Multiple Dao Persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can Watch "Can New topic

Can't Retrieve EntityManager With Multiple Dao Persistence

Eugene Williams

Joined: Jan 31, 2010
Posts: 20
I'm using Struts 2, Spring, Hibernate+JPA, with Generic definitions for the Daos and Services (i.e. GenericDao<T, ID> and GenericService<T>). Up to this point I've been able to handle basic lists within my views. As I'm proceeding with development of the application, I now need to edit the data.

When generating the form to do this (the form is generated from a view which requires a method in the ClothingAction >> setupForInsertUpdate() ) I need to generate a list of clothing types during the setup process so that my list of options in the form can be populated as provided by the DB (accessed via ClothingTypesDao). The problem I'm having is that when I create a service within my action to handle this list creation (ClothingTypeService) and try to process the method within it called getAllClothingTypes() which uses the EntityManager defined within the GenericDao, the EntityManager is not available (most likely because it's been detached, or closed, I'm assuming).

I can post code later, but can anyone provide general insight into how I can have my ClothingTypeDao access the same EntityManager as my ClothingAction > ClothingService > ClothingDao object during that "setupForInsertUpdate()" method defined in the ClothingAction?

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Please don't duplicate post or resurrect old threads. Either post a new thread or reply to a thread, please don't do both.

In JavaRanch's case, both threads are now at the top, and are duplicates now. If you just do one, then people will see it. They shouldn't be wasting time having to see it now twice.

Thanks for understanding, I am going to close the resurrected thread and leave this thread.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Eugene Williams

Joined: Jan 31, 2010
Posts: 20
I'm new.

Thanks sir
Eugene Williams

Joined: Jan 31, 2010
Posts: 20
Found the solution to my problem. In order to obtain an entity manager for all DAOs defined within the context of the application, I needed to access the ApplicationContext before generating my supplemental data request.

this required that I pull the bean from those defined in the applicationContext.xml file, rather than using a DAO object instantiated within the struts action.
so, rather than

setupForInsertUpdate now contains the following lines:

my applicationContext.xml file is stored in my WEB-INF/ folder - the above request forced it upon the classpath so that it was retrievable by the ClassPathXml... convenience class
now that I have an object which gives me access to the app context, I can now grab beans which are defined within it, namely the DAOs I need
I agree. Here's the link:
subject: Can't Retrieve EntityManager With Multiple Dao Persistence
It's not a secret anymore!