Search code examples
ignite

column type conversion when using RDMS and Ignite Integration


I am using RDMS and Ignite Integration https://apacheignite-mix.readme.io/v1.7/docs/automatic-persistence to transform each table record into POJO and put the POJO into Ignite Cache.

I want to change the type of a column to anther type for the corresponding field in POJO.

For example, I want to change the BIRTH_DAY column which is string type is db to the corresponding field which is java.sql Date in POJO.

I would ask how to this? Can I do the conversion logic in the CacheConfig code side?

Following is piece of the code that do the mapping between column name and POJO field name,but it looks that I can't add in the conversion logic?

// Value fields for TBL_PERSON
Collection<JdbcTypeField> vals = new ArrayList<>();
vals.add(new JdbcTypeField(Types.INTEGER, "ID", int.class, "id"));
vals.add(new JdbcTypeField(Types.VARCHAR, "NMAE", String.class, "name"));
vals.add(new JdbcTypeField(Types.VARCHAR, "BIRTHDAY", String.class, "birthday"));

Solution

  • You may try to create your custom JdbcTypesTransformer and set it via factory (see: CacheJdbcPojoStoreFactory#setTransformer). Also note, that you will need to deploy class with your custom transformer on all nodes. Code could be like this:

    public class MyJdbcTypesTransformer extends JdbcTypesDefaultTransformer {
    /** */
    private static final long serialVersionUID = 0L;
    
    /** {@inheritDoc} */
    @Override public Object getColumnValue(ResultSet rs, int colIdx, Class<?> type) throws SQLException {
        Object val = rs.getObject(colIdx);
    
        if (val instanceOf Strinng && type == Date.class) {
          .... conver from string to date....
        }
    
        return super.getColumnValue(rs, colIdx, type);
    }
    

    Also JdbcTypesDefaultTransformer (default transformer) will try to convert types using underlying JDBC diver (for example, that means if in DB you have Integer, you can try to get it in Java as String). For this you should change mappings manually in config:

    vals.add(new JdbcTypeField(Types.INTEGER, "ID", String.class, "id"));
    

    For String -> Date this may not work out of the box, because driver and JdbcTypesTransformer have no idea how date was encoded in string.