jeudi 25 juillet 2019

BeanPropertyRowMapper cache problem on Tomcat

I am using my custom row mapper extended from BeanPropertyRowMapper to override underscoreName method. It was working well. I added new fields to DTO class and deployed to Tomcat again. But it couldn't map new fields. In BeanPropertyRowMapper's initialize method it is using BeanUtils.getPropertyDescriptors(mappedClass). I think the problem is due to not updated cache on deploy.

I tried to restart tomcat but it didn't work.

@Slf4j
public class ColumnRowMapper<T> extends BeanPropertyRowMapper<T> {

  private ColumnRowMapper(final Class<T> mappedClass) {
    super(mappedClass);
  }

  @Override
  protected String underscoreName(final String name) {
    Field declaredField = getField(getMappedClass(), name);
    String columnName = getColumnName(declaredField);
    return columnName == null ? super.underscoreName(name) : columnName.toLowerCase();
  }

  private Field getField(Class<?> clazz, String name) {
    if (clazz == null) {
      return null;
    }
    Field field = null;
    try {
      field = clazz.getDeclaredField(name);
    } catch (Exception e) {
      field = getField(clazz.getSuperclass(), name);
    }
    if (field == null) {
      log.warn("Ups, field «{}» not found in «{}».", name, clazz);
    }
    return field;
  }

  private String getColumnName(Field declaredField) {
    Column annotation;
    String columnName;
    if (declaredField == null || (annotation = declaredField.getAnnotation(Column.class)) == null
        || StringUtils.isEmpty(columnName = annotation.name())) {
      return null;
    }
    return columnName;
  }

  public static <T> BeanPropertyRowMapper<T> newInstance(final Class<T> mappedClass) {
    return new ColumnRowMapper<>(mappedClass);
  }

}





Aucun commentaire:

Enregistrer un commentaire