I am working on a Spring Boot project using JdbcTemplate
to access data and I have the following problem with a single specific field using BeanPropertyRowMapper
to map the result of a query on a DTO object.
This is my repository method performing my query and mapping the query result on a DTO object:
@Override
public OneRowReadTrain1DTO getOneRowReadTrain1DTO() {
String SELECT_SINGLE_RECORD_OneRowReadTrain1 = "SELECT * FROM OneRowReadTrain1";
List<OneRowReadTrain1DTO> resultList = jdbcTemplate.query(SELECT_SINGLE_RECORD_OneRowReadTrain1,
BeanPropertyRowMapper.newInstance(OneRowReadTrain1DTO.class));
OneRowReadTrain1DTO result = resultList.get(0);
return result;
}
It works fine except a specific field. The problem is that this field is the only one that doesn't have the same name as DTO class field and column name on my table.
Infact on my OneRowReadTrain1DTO
I have:
public class OneRowReadTrain1DTO {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = JsonFormat.DEFAULT_TIMEZONE)
private Date timeStamp;
..........................................................
..........................................................
..........................................................
}
As you can see this class field is named as timeStamp
but the related column on my table is Time_Stamp
so this field is valorized as null
.
There is a way to avoid to write a custom row mapper (the class fields\table columns are hundreds) and to specify that my Time_Stamp
have to be mapped on the timeStamp
?
I know that I can simply rename this field name in my class but it is pretty ugly.
Instead of using "select *" you can select each field required for the DTO and give it the proper name of the field.
e.g.: "select Time_Stamp as timeStamp, etc... "