从批处理参数获取输入查询

时间:2016-06-30 12:12:34

标签: spring jpa spring-batch

我需要动态地从批处理参数中检索参数并将其设置为我的查询。这是我的实施:

<bean id="Reader" scope="step"
    class="org.springframework.batch.item.database.JpaPagingItemReader">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />

    <property name="queryString"
        value="SELECT distinct d FROM ReglementClient r 
                   JOIN FETCH d.etablissement e 
                   WHERE e.code = #{jobParameters[code]}
                   And r.statut in('Validé')
                " />

</bean>

我运行我的批处理使用以下参数:-Dcode = 882。

我遇到以下错误:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: And near line 1, column 697 [SELECT distinct d FROM com.natixis.smartcontestation.emetteur.entite.ReglementClient r JOIN FETCH d.etablissement e And r.statut in('Validé') And e.code =  ]
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
    at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:281)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:93)

2 个答案:

答案 0 :(得分:1)

你需要在jpql中指定正常的参数语法,使用阅读器上的parameterValues属性来设置jobParameter

<bean id="Reader" scope="step"
      class="org.springframework.batch.item.database.JpaPagingItemReader">
    <property name="entityManagerFactory"
              ref="entityManagerFactory" />

    <property name="queryString"
              value="SELECT distinct d FROM ReglementClient r 
               JOIN FETCH d.etablissement e 
               WHERE e.code = :code
               And r.statut in('Validé')
            " />

    <property name="parameterValues">
        <map>
            <entry key="code" value="#{jobParameters[code]}"/>
        </map>
    </property>

</bean>

答案 1 :(得分:0)

如果您使用run jobs from the commandline的标准弹簧批处理机制,则需要遵循作业参数的正确模式

  

这些参数必须首先传入路径和名称   第二。这些之后的所有参数都被认为是JobParameters   并且必须采用&#39; name = value&#39;:

的格式
bash$ java CommandLineJobRunner endOfDayJob.xml endOfDay schedule.date(date)=2007/05/05