I'm having trouble understanding issues that seem to be linked to the persistence context difference between the applicationContext and dispatcher-servlet context.
My applicationContext.xml and dispatcher-servlet.xml is posted in the second post in this thread.
I have a generic DAO, a service package with and interface called "ServiceService", and an impl package underneath it with "ServiceServiceImpl".
My class definition looks like this:
I copied all of this from an example, so I'm started to get confused if that class is really a service or if it's the actual DAO....
public class ServiceServiceImpl extends GenericDAOWithJPA<Service, Integer> implements ServiceService
Either way, if I try to create Service objects from within that DAO and persist them, hibernate generates and ID and no errors are thrown, however, through my Spring controller which has this DAO auto-wired, if I do a findAll(), I get no results.
Then, if I build a service object in my controller, then persist it through the serviceDao object in the controller, a findAll() returns this object with the incremented ID (as if the persist command that I did from inside the DAO actually worked, but it just doesn't show up in the results).
Every time I make a small change to my .xml files, things go haywire. I've read till my eyes bleed and still can't seem to find a consensus on how to use <context:annotation-config/>, <tx:transaction-driven>, <mvc:annotation-driven>, etc etc.
To get into the weeds a little more, one of my goals was to hold a map in-memory of Service objects. On the page that displays values, it will make an AJAX call to my controller and retrieve these objects from the map, so it doesn't have to execute queries each time the page is accessed. These values are not likely to change very often at all. There will be an administration page where an admin can persist the services currently in the map to the database, add new services to the map, or rebuild the map from persistence. So when my webapp starts up, I need to query another web service to get my initial list of Services... I created a method annotated with @PostConstruct in my ServiceServiceImpl class to fetch them, then persist them. When I do this, I can't do a findAll() and see any of them from my controller. If I call that method to fetch the Services and persist them from the controller, where all of the logic is in that method in my ServiceServiceImpl class, I still can't see them. But if I separate the logic just a little bit and call a method from my controller to ServiceServieImpl to a) fetch the list from the remote service, b) loop through that list, call get the map from the ServiceServiceImpl class and add them one by one, and finally c) call a method in ServiceServiceImpl to persist that map to the database (loops through the map and calls super.entityManager.persist(Service), it works and from my controller, I can do a findAll() and see those services.
I'm pretty sure my xml files are properly structured so that all of my annotated beans EXCEPT controllers are loaded in the applicationContext, and only the controllers are loaded in the dispatcher-servlet context. From my understanding, the dispatcher-servlet should have visibility into the application context (not vice versa). So I don't understand why entities that are created/persisted from within one of my service/dao objects seem to be invisible to my dispatcher-servlet context.
I think I've blurred the lines between the DAO and the service layer -- my ServiceServiceImpl contains quite a bit of logic and various methods to either retrieve objects from the local map or to persist/query via it's parents entityManager. Any advice to help me sort out this mess... create better distinction between service layer and dao layer, and figure out what the heck is going on with contexts/visibility would be greatly appreciated.