1

I am working in a project where we are using Spring batch framework. I am novice to it.

I have a task which is like reading a fixed length flat file length and then process it and populate some bean and after that using some value from the request I have to fetch data from Database using Oracle and then generate some response as a fixed length flat file.

I have pasted below the little code snippet from configuration file.

<bean name="tickerReader"
        class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" ref="fileSystemResource" />
        <property name="lineMapper" ref="tickerLineMapper" />
<bean>
 ..............................
 ..............................

<batch:job id="TickerPriceConversion">
        <batch:step id="convertPrice">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="tickerReader" processor="tickerPriceProcessor"
                    writer="simbeqResponseFlatFileWriter" commit-interval="10" >
            </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>

I have done the reading part i.e. reading the request file and populated the corresponding bean. Now I need to fetch data from Oracle using hibernate corresponding to the data from the request. I am not sure how to do it? How should i go about it means configuring session factory and using it to fetch data.

Can someone please guide me?

2 Answers 2

2

You may use HibernateCursorItemReader as in the sample hibernate job

<bean id="hibernateItemReader"
    class="org.springframework.batch.item.database.HibernateCursorItemReader">
    <property name="queryString" value="from CustomerCredit" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

For setting up sessionFactory and transaction manager, you may also look at the sample hibernate-context

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingLocations" value="classpath*:/org/springframework/batch/sample/domain/**/*.hbm.xml" />
    <property name="hibernateProperties">
        <value>
            <![CDATA[
        hibernate.show_sql=true
        hibernate.format_sql=true
        ]]>
        </value>
    </property>
</bean>
Sign up to request clarification or add additional context in comments.

1 Comment

I accidentally downvoted this answer yesterday, sorry, and now the vote is locked! :( Could you edit it so that I can undo my downvote?
0
@StepScope
@Slf4j
@Component
public class CursorItemReader extends HibernateCursorItemReader<ItemEntity> {


    public CursorItemReader(EntityManagerFactory entityManagerFactory,
                            @Value("#{stepExecution}")StepExecution stepExecution) {

       
        this.setName("CursorItemReader");
        this.setSessionFactory(entityManagerFactory.createEntityManager().unwrap(org.hibernate.Session.class).getSessionFactory());
        this.setQueryString("from ItemEntity");
        this.setUseStatelessSession(true);
        this.setFetchSize(5);
    }

    @Override
    public ItemEntity read() throws Exception {
        ItemEntity item = this.doRead();
        return item;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.