Announcement Announcement Module
Collapse
No announcement yet.
How to retrieve column names of HibernateCursorItemReader for FlatFileItemWriter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to retrieve column names of HibernateCursorItemReader for FlatFileItemWriter

    I'm trying to export data from a database with spring-batch and hibernate. Actually the strong requirement is that the query is a stored procedure returning a cursor. This query and resulting columns can change from time to time.
    HTML Code:
    <sql-query name="reportingQuery" callable="true">
        { call GET_REPORTING_QUERY(?) }
    </sql-query>
    So I wrote a simple job with an HibernateCursorItemReader and a FlatFileItemWriter with a DelimitedLineAggregator :
    HTML Code:
    <batch:step id="extractionStep">
        <batch:tasklet>
            <batch:chunk skip-policy="defaultSkipPolicy" commit-interval="1000">
                <batch:reader>
                    <bean class="org.springframework.batch.item.database.HibernateCursorItemReader">
                        <property name="queryName" value="reportingQuery" />
                        <property name="sessionFactory" ref="sessionFactory" />
                        <property name="useStatelessSession" value="true" />
                    </bean>
                </batch:reader>
                <batch:writer>
                    <bean id="globalQueryItermWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
                        <property name="resource" value="output.csv" />
                        <property name="lineAggregator">
                            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                                <property name="delimiter" value="," />
                            </bean>
                        </property>
                    </bean>
                </batch:writer>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
    my issue, is that this way there is no header line in the output.csv file. I've tried to set fieldExtractor of the line aggregator to a BeanWrapperFieldExrtactor, but I got to set the names. Setting the names doesn't help much more, because hibernate cursor reader only returns an object[] and not a fieldset and then I have an invalid property error.
    HTML Code:
    <property name="fieldExtractor">
      <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
        <property name="names" value="ID,NAME,DATE,..."></property>
      </bean>
    </property>
    So I have basicly two questions :
    1. Is there a way to retrieve the columns names from the hibernate reader and inject it as header of the output file?
    2. How to force hibernate query to return a fieldset (with unknown columns)?

    thanks for your help.

  • #2
    6 month since I've asked this question, no answer, no comment, nothing, by community member or spring developper. It seems that spring is a moribond project with no community reactivity. Even no one to tell me that my question is not valid, nor understandable. it is likely that spring won't be a framework of choice for further projects.

    Comment


    • #3
      With regards to your questions:
      1. No. The reason people use Hibernate is to insulate the code from the underlying database structure. By exposing that database structure, you quickly loose the benefits of Hibernate.
      2. HibernateCursorItemReader only returns an Object [] if cursor.get() returns more than one object, in which case, we assume that the results are a Projection. In that case, you can implement your own FieldExtractor that maps the elements of the list to the fields you want in the writer. A FieldSet is a data structure specific to file I/O. We created it to provide ResultSet type semantics to file reading.

      Comment

      Working...
      X