title: Using Spring+Hibernate for testing purposes
body: <p>I want to clarify my problem a bit more:</p>
  
  <ol>
  <li><p>I understand the purposes of using SPring Framework (i.e. container-managed object lifecycle) and also Hibernate (using ORM between Javaobjects and relational database systems - impedance mismatch resolution).</p></li>
  <li><p>I understand how we autowire an object and Spring takes over the creation and destruction of the object during runtime by looking at the <code>applicationContext.xml</code> file (in addition to persistence.xml file if using Hibernate or any other persistence provider).</p></li>
  </ol>
  
  <p>What I want to do is the following:</p>
  
  <ol>
  <li><p>I would like to implement my own shopping service. I already have entity (item) annotated with @Table, @Id, @Column, etc. to tell JPA that this is what will be stored in the database.</p></li>
  <li><p>I already have a DAO interface (currently only add and delete methods) implemented by a DaoImpl class where I have done the following:</p>
  
  <pre><code>@Repository
  @Transactional
  public class MyShopDbDaoImpl implements MyShopDbDao {
      // The following named unit will be in my persistence.xml file
      // Which will be placed in src/main/resources/META-INF folder
      @PersistenceContext(unitName="myShopDbDao")
      private EntityManager em;
  
      // Getters for em (simply returns em)
  
      // Setters for em (simply assigns an em supplied in the args.)
  
  
      // Other query method
  }
  </code></pre></li>
  <li><p>I also have a ShopDbController controller class that uses:</p>
  
  <pre><code>@Autowired
  // defined in the applicationContext.xml file
  private MyShopDbDao myShopDbDaoImpl
  </code></pre></li>
  </ol>
  
  <p>What I am struggling with is the "Understanding" of <code>EntityManagerFactory</code> and <code>EntityManager</code> relationships along with how the transactions must be managed. I know that the following hierarchy is the main starting point to understand this:</p>
  
  <ol>
  <li><p>Client talks to a controller.</p></li>
  <li><p>Controller maps the request and gets the entitymanager to do queries and stuff to the database (either a test/local database with JUNIT test etc. or an actual SQL-type database).</p></li>
  </ol>
  
  <p>What I do know is that transactions can be managed either manually (i.e. beginning, committing, and closing a session) or through Spring container (i.e. using bean defs in applicationContext.xml file). How can I get more information about the entitymanagers and entitymanagerfactory in order to setup my system?</p>
  
  <p>I didn't find the online documentation from Oracle/Spring/Hibernate very helpful. I need an example and the explanation on the relationship between entitymanagerfactory, sessionfactory, entitymanager, and transactionmanager. Could someone please help me with this?</p>
  
  <p>I don't need people to hold my hand, but just put me in a right direction. I have done Spring projects before, but never got to the bottom of some stuff. Any help is appreciated.</p>