java Spring批处理中使用hibernate读取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14460198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:21:16  来源:igfitidea点击:

Using hibernate in Spring batch to read data

javaspring-batch

提问by Anand

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

我在一个使用 Spring 批处理框架的项目中工作。我是新手。

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.

我有一个任务就像读取固定长度的平面文件长度,然后处理它并填充一些 bean,然后使用请求中的一些值我必须使用 Oracle 从数据库中获取数据,然后生成一些响应作为固定长度的平面文件。

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.

我已经完成了读取部分,即读取请求文件并填充相应的 bean。现在我需要使用与请求中的数据相对应的休眠从 Oracle 获取数据。我不知道该怎么做?我应该怎么做意味着配置会话工厂并使用它来获取数据。

Can someone please guide me?

有人可以指导我吗?

回答by Serkan Ar?ku?u

You may use HibernateCursorItemReaderas in the sample hibernate job

您可以在示例休眠作业中使用HibernateCursorItemReader

<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

要设置 sessionFactory 和事务管理器,您还可以查看示例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>