【问题标题】:Spring-batch Entity Manager becomes null after initSpring-batch Entity Manager 在初始化后变为空
【发布时间】:2016-02-15 10:24:50
【问题描述】:

我目前正在实现一个读取和写入文件的 Spring-batch,但还需要对数据库执行 CRUD 操作。

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

谁能帮我解决这个问题? (通过可用的东西提供解决方案或链接将是完美的)。

我的 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;
}
}

有一些代码被注释掉了,因为我尝试了多种方法(更改持久性上下文类型,“手动”恢复实体管理器,拥有 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

在调试时,当我在 DAO 中调用“createQuery”时,EntityManager 会抛出空指针。

感谢您的帮助。 我会继续寻找。上帝的速度!

【问题讨论】:

  • 它不能是null(假设你有&lt;context:component-scan /&gt;&lt;context:annotation-config /&gt;!)spring会在启动时抛出异常。只有null 配置错误(即没有注释处理)或者您自己创建实例。
  • 我想我明白了。当我从构造函数调用它时它返回 null 但是当我在另一个方法中“点击它”时......在我的情况下,“doRead”它按预期工作。我将使用所有 CRUD 方法进行测试并确认。似乎每次我在这里发布一些东西,我都会在 5 分钟后修复它.. :)

标签: spring-batch entitymanager


【解决方案1】:

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

在“doRead()”方法中移动服务调用后,我可以使用 EntityManager 执行所有 CRUD 操作。

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

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 2013-11-26
    • 2014-03-19
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    相关资源
    最近更新 更多