3 Replies Latest reply on Nov 12, 2012 12:24 PM by splatch

    Can Camel SQL Gateway be configured to store entire object in a table

    jeffdelong

      I have a Switchyard application, with one of the services a Bean service that uses JPA to store the values of a Java object in a table. I want to try the same thing using the Camel SQ Gateway, but not sure what the query should look like. This is the only example in the quickstarts:

       

      <camel:query>INSERT INTO greetings (name) VALUES (#)</camel:query>

       

      but it inserts the message string into the name column. Instead of a string in the message body, I have a custom Java object.

        • 1. Re: Can Camel SQL Gateway be configured to store entire object in a table
          splatch

          Hey Jeff,

          The quickstart is really simple, but logic behind in Camel is not really complicated. Camel converts message payload to Iterator and binds parameters using 0...n position. If your object have more than one field then you need a custom type converter. Detailed instructions you can find in Camel docs: http://camel.apache.org/type-converter, but example can be really easy:


          @Converter
          public class MyConverter {
          
              @Converter
              public static Iterator toInputStream(MyPOJO pojo) {
                  return new MyPOJOIterator(pojo);
              }
          }
          
          class MyPojoIterator implements Iterator {
              private int position = 0;
              private MyPOJO pojo;
          
              public Object next() {
                  switch (object++) {
                      case 0: return pojo.getId();
                      case 1: return pojo.getName();
                      case 2: return pojo.getDate();
              }
          }
          
          

           

          Remember to add META-INF/services/org/apache/camel/TypeConverter with MyConverter class name.

           

          Kind regards,

          Lukasz

          • 2. Re: Can Camel SQL Gateway be configured to store entire object in a table
            jeffdelong

            Thanks Lukasz.

             

            So what would the camel query string look like in this case?

             

            Thanks

            • 3. Re: Can Camel SQL Gateway be configured to store entire object in a table
              splatch

              Hey Jeff

              Query then will look like this

               

              <camel:query>INSERT INTO greetings (name,person,age) VALUES (#,#,#)</camel:query>
              

               

              Iterator should return values in same order as parameters are used (name, person, age).