public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();

    if (queryProvider == null) {
      Assert.notNull(entityManagerFactory);
      Assert.hasLength(queryString);
    }
    // making sure that the appropriate (JPA) query provider is set
    else {
      Assert.isTrue(queryProvider != null, "JPA query provider must be set");
    }
  }
  @Override
  protected void doOpen() throws Exception {
    super.doOpen();

    entityManager = entityManagerFactory.createEntityManager(jpaPropertyMap);
    if (entityManager == null) {
      throw new DataAccessResourceFailureException("Unable to obtain an EntityManager");
    }
    // set entityManager to queryProvider, so it participates
    // in JpaPagingItemReader's managed transaction
    if (queryProvider != null) {
      queryProvider.setEntityManager(entityManager);
    }
  }
 @Override
 protected void doClose() throws Exception {
   entityManager.close();
   super.doClose();
 }