Announcement Announcement Module
Collapse
No announcement yet.
Spring Batch 2.0 - preparedStatementSetter or setParameter or in HibernateCursorItem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch 2.0 - preparedStatementSetter or setParameter or in HibernateCursorItem

    My Friends.

    I'm using HibernateCursorItemReader but in time to crate queryString I came cross the problem at not having as create queryString using setParameter in HibernateCursorItemReader.
    Really we haven't other form to create queryString unless concatenate strings with variables?
    Any peoples have one sugestion for me?

    Thanks a lot Guys .

    Regards.

    Bruno Gambier

  • #2
    If the query parameter values come from the job context you can use late binding, e.g.

    Code:
    <property name="queryString">
      <value><![CDATA[
    from Foo where name = '#{jobParameters[fooName]'}
    ]]>
      </value>
    </property>
    Last edited by Dave Syer; Jun 19th, 2009, 05:08 AM. Reason: added quotes

    Comment


    • #3
      Thanks

      Originally posted by Dave Syer View Post
      If the query parameter values come from the job context you can use late binding, e.g.

      Code:
      <property name="queryString">
        <value><![CDATA[
      from Foo where name = '#{jobParameters[fooName]'}
      ]]>
        </value>
      </property>
      Master Dave.

      Thanks a lot man, is worked.

      Regards.

      Bruno Gambier

      Comment


      • #4
        Skip parameter of the query with [CDATA []] and jobParameters []

        Hello Dave.

        Thank you for instruction but I am trying to use it in the "bean" from my job but he is reading # (jobParameters [id]) as a string.
        The difference between your example and my xml is that I'm using the xml tag "" <beans: bean> ", so even thought you should use the <! [CDATA [the other way and did some tests but could not get any success.

        I am putting below the tests using the <! [CDATA [with my xml using "<beans: bean>.

        You or someone had an idea of how to use <! [CDATA [# (jobParameters with []) in an xml schema that uses <beans: bean>?

        My xml job concatenate the instruction that the query string jobParameter
        Code:
        <beans:beans xmlns="http://www.springframework.org/schema/batch"
        	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xsi:schemaLocation="
                   http://www.springframework.org/schema/beans 
                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                   http://www.springframework.org/schema/batch 
                   http://www.springframework.org/schema/batch/spring-batch-2.0.xsd">
        	<beans:import resource="simple-job-launcher-context.xml" />
        
        	<job id="requesterElectronicInvoice">
        		<step id="step1">
        			<tasklet transaction-manager="transactionManager">
        				<chunk reader="hibernateItemReader" writer="itemWriter"
        					commit-interval="10" />
        			</tasklet>
        		</step>
        	</job>
        
        	<beans:bean id="ReaderInvoice" class="br.com.moip.jobs.rnf.ReaderInvoice">
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        
        	<beans:bean id="hibernateItemReader"
        		class="org.springframework.batch.item.database.HibernateCursorItemReader">
        		<beans:property name="queryString">
        			<beans:value>
        				 <![CDATA[from PaymentUsed where id = '#{jobParameters[id]}' ]]>
        			</beans:value>
        		</beans:property>
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        
        	<beans:bean id="itemWriter" class="br.com.moip.jobs.rnf.WriterInvoice">
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        </beans:beans>
        System out of the executed query where the parameter was concatenated
        Code:
        where
                paymentuse0_.id='#{jobParameters[id]}'
        my tests the movement of parameters that have not worked
        Code:
        <beans:bean id="hibernateItemReader"
        		class="org.springframework.batch.item.database.HibernateCursorItemReader">
        		<beans:property name="queryString">
        			<beans:value>from PaymentUsed where id = <![CDATA[#{jobParameters[id]}]]></beans:value>
        		</beans:property>
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        Code:
        <beans:bean id="hibernateItemReader"
        		class="org.springframework.batch.item.database.HibernateCursorItemReader">
        		<beans:property name="queryString">
        			<beans:value>
        				 <![CDATA[from PaymentUsed where id = #{jobParameters[id]} ]]>
        			</beans:value>
        		</beans:property>
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        Code:
        <beans:bean id="hibernateItemReader"
        		class="org.springframework.batch.item.database.HibernateCursorItemReader">
        		<beans:property name="queryString" value="from PaymentUsed where id = '#{jobParameters[id]}'"/>			
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>
        Code:
        <beans:bean id="hibernateItemReader"
        		class="org.springframework.batch.item.database.HibernateCursorItemReader">
        		<beans:property name="queryString" value="from PaymentUsed where id = #{jobParameters[id]}"/>			
        		<beans:property name="sessionFactory" ref="sessionFactory" />
        	</beans:bean>

        Comment


        • #5
          Your reader needs to be in step scope. See here for more information: http://static.springsource.org/sprin...l#late-binding.

          Comment


          • #6
            Spring Batch 2.0 - preparedStatementSetter or setParameter or in HibernateCursorItem

            Hello Dave Syer.

            Thanks a lot for your answer, I read your suggestion but this example is used by read one file stating onde bean property.

            I need to pass a parameter of inial date and within the final declaration of the query string.

            My line of jobLauncher:

            Code:
            JobParameters jobParameters = new JobParametersBuilder().addDate("beginDate", Convert.string2date("01/02/2009")).addDate("endDate", Convert.string2date("28/02/2009")).toJobParameters();
            and my de below my query where I need pass this two parameter in clasule where:


            Code:
            <beans:bean id="readerPayment" class="br.com.moip.jobs.rnf.ReaderPayment">
            	 		<beans:property name="itemReader" ref="itemReaderPayment"/>	 		
            	 	</beans:bean>
            	 			
            		<beans:bean id="itemReaderPayment"
            			class="org.springframework.batch.item.database.HibernateCursorItemReader">		
            			<beans:property name="sessionFactory" ref="sessionFactory" />
            			<beans:property name="useStatelessSession" value="false"/>						
            			<beans:property name="queryString">
            				<beans:value><![CDATA[
            					from PaymentUsed p where p.status = 1 and p.installments is empty and p.date >= :beginDate and p.date < :endDate 
            				]]>
            				</beans:value>								
            			</beans:property>	
            		</beans:bean>
            You would have one suggestion of this?

            Comment


            • #7
              Code:
              <bean id="itemReaderPayment"
              class="org.springframework.batch.item.database.HibernateCursorItemReader" scope="step">		
              	<property name="sessionFactory" ref="sessionFactory" />					
              	<property name="queryString">
              		<value><![CDATA[
              			from PaymentUsed p where p.status = 1 and p.installments is empty 
              			  and p.date >= #jobParameters['beginDate'] and p.date < #jobParameters['endDate'] 
              		]]>
              		</value>								
              	</property>	
              </bean>

              Comment

              Working...
              X