This is to have an understanding of how de-serialization works when JdbcTemplate converts the query result into object form.
My Entity Structure looks as show below,
If you observe, there is a variable name "month" whose column name is "monitor_month". This means it refers to "month" at Object level and "monitor_month" as database column name (as we know @Entity refers to a table).
Now a piece of module was developed in order to retrieve data from database. This can either be done by using JPARepository or by using JdbcTemplate. I am using the later approach,
Below is the piece of code which I have used to retrieve database information and de-serialize the information in the entity shown above,
The targetClassName here refers to the entity shown above. Considering no exception case, this piece of code should retrieve information from database by executing the query which is present in "queryTemplate" and de-serialize it by using the className provided in the variable "targetClassName".
But somehow all of the data are not being de-serialized. Interestingly only the fields whose column names and variables are same (Ignore underscores and case) are being serialized, i.e., monitor_year is being deserialized because monitor_year and monitorYear are one and the same variable.
Hence it is must and should that the variable name which is being used should be equivalent to the column name that is being defined.
I have modified the entity as shown below
Once I have modified the entity as shown above, I was successful in fetching the result.
JdbcTemplate when executes the query gets the information which has meta-data of database (i.e., column names as of table column names). But now we need to de-serialize into an object format. Hence jdbcTemplate would want a kind of understanding between the variable names and the column names and hence it will assume these both are equivalent variables. This means that the column name "monitor_year" is equivalent to "monitoryear" or "MonitorYear" or MoniTORYear" or simply "monitorYear". Hence when there is a database column monitor_year, it will push the value into any of the above mentioned variables.
The problem with my previous approach was, the query result was giving response in the column "monitor_day" where as my variable's name is "day". This means the jdbcTemplate would consider "monitor_day" and "day" as two different variables and hence it will not de-serialize this field. The same was the problem with other fields as well.
Post a Comment