3 Replies Latest reply on Sep 17, 2018 1:42 PM by cfang

    jdbcItemWriter with custom class

    richardmoore

      We have a beanIOItemReader that is loading our custom bean class and we would like to write it out in a jdbcItemWriter. Do you have an example? Do you have to annotate the class or will the parameterNames just be the name less the "get"?

        • 1. Re: jdbcItemWriter with custom class
          cfang

          You can either annotate the bean class, or specify parameterNames and optionally parameterTypes in job xml.

           

          For custom bean class without annotations, jdbcItemWriter constructs a map whose keys are the bean class property names, and whose values are the corresponding property values. 

           

          The closes example is intro-jberet/csv2db.xml at master · jberet/intro-jberet · GitHub , where the beanType is java.util.Map.  But I think it's similar to your case where the beanType is a custom bean type.

           

          To write another example,  if your insert sql is

           

          insert into person (id_number, full_name) VALUES (?, ?)

           

          and your Person class is:

           

          public class Person {

              private long id;

              private String name;

           

             public long getId() {...}

           

             public String getName() {...}

           

          }

           

          The parameterNames should be ="id, name"

          parameterTypes should be          = "Long, String"

          • 2. Re: jdbcItemWriter with custom class
            richardmoore

            If it were annotated would be something like:

             

            @JsonProperty("Id")

            private long id;

            @JsonProperty("Name")

            private String name;

            • 3. Re: jdbcItemWriter with custom class
              cfang

              an example of class with jackson annotations:

               

              jberet-support/StockTradeBase.java at master · jberet/jberet-support · GitHub

               

              If you use annotations and don't use parameterNames batch property, jdbcItemWriter will take column names from sql statement as keys to look up in the bean properties of the java class.  In the above example, jdbcItemWriter will try to look up "id_number" and "full_name" in bean class.  So you will need to map to

               

              @JsonProperty("id_number")

               

              @JsonProperty("full_name")

               

              So the easiest way is to keep table column names and bean class property names consistent, to avoid the need for mapping.