Spring-batch Entity Manager becomes null after init
<p>I'm currently implementing a Spring-batch that reads and writes to files BUT also needs to do CRUD operations on a database.</p>

<p>I've tried to simply define an Entity manager in my xml configuration, and use it in my DAO class. However, right after the init, the EntityManager becomes null. </p>

<p>Can anyone help me with this ? (a solution or a link via something usable would be perfect).</p>

<p>My batchContext.xml</p>

<pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
    &lt;property name="driverClassName" value="${batch.datasource.driverClassName}"/&gt;
    &lt;property name="url" value="${batch.datasource.url}"/&gt;
    &lt;property name="username" value="${batch.datasource.username}"/&gt;
    &lt;property name="password" value="${batch.datasource.password}"/&gt;
    &lt;property name="testOnBorrow" value="true"/&gt;
    &lt;property name="testOnReturn" value="true"/&gt;
    &lt;property name="testWhileIdle" value="true"/&gt;
    &lt;property name="timeBetweenEvictionRunsMillis" value="1800000"/&gt;
    &lt;property name="numTestsPerEvictionRun" value="3"/&gt;
    &lt;property name="minEvictableIdleTimeMillis" value="1800000"/&gt;
&lt;/bean&gt;

&lt;bean id="entityManagerFactory" name="entTransactionMgr" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;!-- &lt;property name="persistenceXmlLocation" value="classpath:/META-INF/spring/persistence.xml" /&gt; --&gt;
    &lt;property name="persistenceUnitName" value="persistenceUnit"/&gt;
    &lt;property name="packagesToScan" value="${jpa.scan.packages}"/&gt;
    &lt;property name="dataSource" ref="dataSource"/&gt;
    &lt;property name="jpaVendorAdapter"&gt;
        &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
    &lt;/property&gt;
    &lt;property name="loadTimeWeaver"&gt;
        &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /&gt;
    &lt;/property&gt;

    &lt;!-- Custom jpaDialect pour le deuxieme batch:job-repository--&gt;
&lt;property name="jpaDialect"&gt;
    &lt;bean class="fr.mma.soecm.batchfacade.util.CustomHibernateJpaDialect" /&gt;
&lt;/property&gt;
    &lt;property name="jpaProperties"&gt;
        &lt;props&gt;
            &lt;!-- multiple props here--&gt;
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
    &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
&lt;/bean&gt;

&lt;!-- mode="aspectj" --&gt;
&lt;tx:annotation-driven  transaction-manager="transactionManager"/&gt;

&lt;batch:job-repository id="jobRepository" data-source="dataSource" transaction-manager="transactionManager"/&gt;

&lt;!-- Jobs held separatelly --&gt;
&lt;import resource="batchContext-job.xml"/&gt;
</code></pre>

<p>My DAO</p>

<pre><code>@Repository("batchJobDao")
@Transactional
public class BatchJobDaoImpl implements BatchJobDao{

@PersistenceContext(unitName="persistenceUnit")
@Autowired
private EntityManager entityManager;

//    @PersistenceContext(unitName="persistenceUnit", type=PersistenceContextType.EXTENDED)
//    public void setEntityManager(EntityManager entityManager) {
//        System.out.println("Setting Entity Manager :" + entityManager);
//        this. entityManager = entityManager;
//    }

@PostConstruct
public void init(){
    if (entityManager == null){
        System.out.println(" Entity Manager is null");
    } else {
        System.out.println(" Entity Manager is not null");
        getAllJobExecutions();
    }
}

private static final String RECHERCHER_JOB_EXECUTION = "Select bje from BatchJobExecution bje";

@SuppressWarnings("unchecked")
@Override
@Transactional("transactionManager")
public List&lt;BatchJobExecution&gt; getAllJobExecutions(){

//      EntityManagerFactory emf=Persistence.createEntityManagerFactory("entTransactionMgr");
//      EntityManager em=emf.createEntityManager();

    Query query = entityManager.createQuery(RECHERCHER_JOB_EXECUTION, BatchJobExecution.class);
    List&lt;BatchJobExecution&gt; executions = (List&lt;BatchJobExecution&gt;) query.getResultList();
    System.out.println("EXES : " + executions);
    return executions;
}
}
</code></pre>

<p>There is some code commented out because I've tried multiple aproaches (changing the persistence context type, recovering the entity manager "manually", having a persistence.xml file for the entityManager) without succes.</p>

<p>My output when running the job is (without all the extra lines..):</p>

<pre><code>Entity Manager is not null
EXES : [fr.mma.soecm.batchfacade.domain.BatchJobExecution@10e6c33,...]
[BatchService] - Synchronous job launch
[AbstractStep] - Encountered an error executing the step
Caused by: java.lang.NullPointerException
</code></pre>

<p>And the null pointer, on debug, is throws by the EntityManager being null when I call the "createQuery" in my DAO.</p>

<p>Thanks for your help.
I'll keep searching on my end.. God Speed!</p>