Spring之后,Spring批处理实体管理器变为null

时间:2016-02-15 10:24:51

标签: spring-batch entitymanager

我目前正在实施一个读取和写入文件的Spring批处理,但也需要对数据库进行CRUD操作。

我试图在我的xml配置中简单地定义一个实体管理器,并在我的DAO类中使用它。但是,在init之后,EntityManager变为null。

任何人都可以帮我吗? (通过可用的东西的解决方案或链接将是完美的)。

我的batchContext.xml

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

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

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<!-- mode="aspectj" -->
<tx:annotation-driven  transaction-manager="transactionManager"/>

<batch:job-repository id="jobRepository" data-source="dataSource" transaction-manager="transactionManager"/>

<!-- Jobs held separatelly -->
<import resource="batchContext-job.xml"/>

我的DAO

@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<BatchJobExecution> getAllJobExecutions(){

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

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

有一些代码被注释掉,因为我已经尝试了多个aproaches(更改持久化上下文类型,恢复实体管理器&#34;手动&#34;,具有entityManager的persistence.xml文件)而没有成功

运行作业时的输出是(没有所有额外的行..):

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

当调用&#34; createQuery&#34;时,EntityManager为空时抛出调试时的空指针。在我的DAO。

感谢您的帮助。 我一直在寻找我的结局......上帝速度!

1 个答案:

答案 0 :(得分:0)

正如上面的评论所述,我的明显问题是由于我试图在步骤的构造函数中调用服务或DAO。

在&#34; doRead()&#34;中移动服务电话后方法,我可以使用EntityManager执行所有CRUD操作。

如果有人对此有任何疑问,请告知我/以及如何使其工作,因为我在互联网上没有找到任何解释,因为我上周开始搜索。