Execution results in JdbcTemplate contain embedded entities

Execution results in JdbcTemplate contain embedded entities


When using JDBCTemplate, you will encounter situations where other entities are embedded in the entity, so that fields in the entity will not be assigned. There are three ways to solve this problem, each of which has its own advantages and disadvantages.
entity

public class User {
    private String name;
    private String password;
    private Father father;
}
class Father {
    private String name;
    private String age;
}

sql

select * from t_user u,t_father f where u.father_id=f.id

java code

jdbcTemplate.query(sql, paramMap, BeanPropertyRowMapper.newInstance(UserVO.class));

One way

Note that embedded entities and entities themselves have the same field, which results in value coverage and different implementations depending on the needs of different entities

public class UserRowMapper implements RowMapper<SubstituteDelivery> {
    @Override
    public UserVO mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserVO userVO = (new BeanPropertyRowMapper<>(UserVO.class)).mapRow(rs,rowNum);
        Father father = (new BeanPropertyRowMapper<>(Father.class)).mapRow(rs,rowNum);
        UserVO.setFather(father );
        return userVO ;
    }
}

Mode two

Column names must correspond exactly to entity fields (including underscore case, which can cause write failures). Universal

public class NestedRowMapper<T> implements RowMapper<T> {
  private Class<T> mappedClass;
  public NestedRowMapper(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
  }
  @Override
  public T mapRow(ResultSet rs, int rowNum) throws SQLException {
    T mappedObject = BeanUtils.instantiate(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
    bw.setAutoGrowNestedPaths(true);
    ResultSetMetaData meta_data = rs.getMetaData();
    int columnCount = meta_data.getColumnCount();
    for (int index = 1; index <= columnCount; index++) {
      try {
        String column = JdbcUtils.lookupColumnName(meta_data, index);
        Object value = JdbcUtils.getResultSetValue(rs, index, Class.forName(meta_data.getColumnClassName(index)));
        bw.setPropertyValue(column, value);
      } catch (TypeMismatchException | NotWritablePropertyException | ClassNotFoundException e) {
         // Ignore
      }
    }
    return mappedObject;
  }
}

Mode three

Need order, field is confusing in many cases

List<UserVO> UserVOS= jdbcTemplate.query("SELECT * FROM message m, user u WHERE u.message_id = m.message_id", new RowMapper<UserVO>() {
    @Override
    public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserVO userVO= new UserVO();
        userVO.setName(rs.getString(1));
        userVO.setPassword(rs.getString(2));

        Father father = new Father();
        father.setName(rs.getString(3));
        father.setAge(rs.getString(4));

        userVO.setFather(father);

        return userVO;
    }
});

Keywords: SQL Java

Added by crazykid on Thu, 03 Oct 2019 22:49:39 +0300