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?
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 found a beautiful pie. And a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop