Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I started to learn spring batch and I have a problem that when i want to persist the state of the job in a database using JobRepositoryFactoryBean . compiler displays :

" Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [springConfig.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/core/simple/ParameterizedRowMapper "

but not error when i use MapJobRepositoryFactoryBean

I'm using spring 5

springconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <context:component-scan base-package="springbatch" />
    <context:annotation-config />
    <bean id="personneReaderCSV" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="input/personnes.txt" />
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="," />
                        <property name="names" value="id,nom,prenom,civilite" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="targetType" value="springbatch.entities.Personne" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean name="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_test" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
                <value>springbatch.entities.Personne</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
    <job id="importPersonnes" xmlns="http://www.springframework.org/schema/batch">
        <step id="readWritePersonne">
            <tasklet>
                <chunk reader="personneReaderCSV" 
                processor="personProcessor"
                    writer="personWriter" 
                    commit-interval="2" />
            </tasklet>
        </step>
    <bean id="daoPersonne" class="springbatch.dao.PersonneDaoImp">
        <property name="factory" ref="sessionFactory"></property>
    </bean>
    <bean id="personWriter" class="springbatch.batch.PersonneWriter">
            <property name="dao" ref="daoPersonne"></property>
    </bean>
    <bean id="personProcessor" class="springbatch.batch.PersonneProcess">
    </bean>
    <bean id="batchLauncher" class="springbatch.MyBean">
    </bean>
    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseType" value="Mysql" />
    </bean>
    <task:scheduled-tasks>
        <task:scheduled ref="batchLauncher" method="message"
            cron=" 59 * * * * * " />
    </task:scheduled-tasks>
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
    </jdbc:initialize-database>
</beans>

but not error when i use :

<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager" /> 
</bean>
                I think the datasource should be a database connection details rather than the scripts which you have mentioned. Try giving the BasicDataSource bean for datasource.
– Rakesh
                Dec 14, 2018 at 11:52
                <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">         <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>         <property name="url" value="${Server}"/>         <property name="username" value="${Username}"/>         <property name="password" value="${Password}"/>         <property name="initialSize" value="2"/>         <property name="maxActive" value="5"/>     </bean>
– Rakesh
                Dec 14, 2018 at 12:27
                Which version of Spring Batch are you using? Looks like ParameterizedRowMapper is not in the spring-jdbc jar you have in your classpath. Can you share your pom.xml or gradle.build to see your dependencies?
– Mahmoud Ben Hassine
                Dec 14, 2018 at 13:37

You are using Spring Batch v2.2 with Spring Framework 5. That's not going to work properly as ParameterizedRowMapper has been removed in Spring Framework 4.2+ (hence the exception).

I recommend that you use Spring Batch v4.1 (since v2.x is not maintained anymore) and your issue should be fixed.

The best way to manage Spring dependencies is to let Spring Boot do it for you either by generating a project from start.spring.io or by using the Spring Boot BOM. With both ways, you will have the correct Spring projects dependencies that are known to work well together.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.