Results 1 to 7 of 7

Thread: Issue using Expression language with jdbc:outbound-channel-adapter

  1. #1
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default Issue using Expression language with jdbc:outbound-channel-adapter

    I am having a simple table structure that stores the message id and the message payload in the table and both are of type varchar.

    i am having the following in the configuration

    Code:
    <jdbc:outbound-channel-adapter data-source="dataSource" channel="outboundJdbcChannelOne"
    	query="insert into MESSAGES (MESSAGE_ID,PAYLOAD) values (:headers[id].toString(),:payload)"
    	sql-parameter-source-factory="spelSource"/>
    									
    	<bean id="spelSource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory"/>
    However, the insersion fails as the expression headers[id].toString() fails.
    Reason being, when the SQL is provided to NamedParameterJdbcTemplate, the named parameter is evaluated to
    headers[id].toString (without the () ) by the NamedParameterUtils as "(" and ")" is a PARAMETER_SEPARATOR and this expression fails to yield a result.

    Am i mising something or on a wrong track, how do i pass spring expression parameters to the query where i need to invoke an adhoc method (not a usual getter, something like toString() for instance) on the object whose return value is to be used to insert or update a record?
    Last edited by Amol Nayak; Nov 14th, 2011 at 12:27 PM.

  2. #2
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    Can anybody help me on this please?

  3. #3
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,029

    Default

    Try this...

    Code:
    <jdbc:outbound-channel-adapter data-source="dataSource" channel="outboundJdbcChannelOne"
    	query="insert into MESSAGES (MESSAGE_ID,PAYLOAD) values (:id,:payload)"
    	sql-parameter-source-factory="spelSource"/>
    		
    <bean id="spelSource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
    	<property name="parameterExpressions">
    		<map>
    			<entry key="id" value="headers['id'].toString()"/>
    			<entry key="payload" value="payload"/>
    		</map>
    	</property>
    </bean>
    The parameter names in the SQL map to expressions in the parameter source factory.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  4. #4
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    Thanks Gary . Yup that's how i managed to do it this morning and it worked fine.

  5. #5
    Join Date
    Aug 2005
    Location
    Atlanta
    Posts
    123

    Default

    Hi Amol,

    Just FYI - I created a Jira to document the usage of SpEL expressions with JDBC Adapters in the Reference Manual.

    https://jira.springsource.org/browse/INT-2244

    In order to improve the situation even further, please monitor: https://jira.springsource.org/browse/INTSAMPLES-33

    In the next day or so, I will add a new JDBC Example to the Spring Integration Samples project at:

    https://github.com/SpringSource/spri...ration-samples that will in fact use SpEL Expressions.

    Thank you very much for raising this question!

    Cheers,
    Gunnar Hillert
    SpringSource/VMWare, Spring Integration team
    SpringSource Team - Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/ghillert
    http://blog.hillert.com/
    http://blog.springsource.com/author/ghillert/

  6. #6
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    Thanks Gunnar
    I'll be more than happy to contribute the sample i am working on. Let me do that tonight once i get back home, i'll put in some comments and make the code more readable and attach it here.

  7. #7
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    Hello Gunnar,
    I have created a jdbc project in the integration samples and added a sample outbound-channel-adapter to it which is using ExpressionEvaluatingSqlParameterSourceFactory.
    You can find the project here, https://github.com/amolnayak311/spring-integration-samples.

    Thanks
    Amol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •