Siavash fallahdoust (2) [Avatar] Offline
#1
Hi
You can use NamedParameterJdbcTemplate instead of JdbcTemplate in chapter 3.
JdbcTemplate is error-prone. The NamedParameterJdbcTemplate is a convenience that allows you to use named parameters.
for example, saveTacoInfo method in Listing 3.9 can be written like this :

taco.setCreatedAt(new Date());
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(taco);
KeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update( "insert into Taco (name, createdAt) values (:name, :createdAt)", namedParameters, keyHolder, new String[]{"id"});
return keyHolder.getKey().longValue();


Regards,
Siavash
570807 (2) [Avatar] Offline
#2
Hi,

thank you for this solution! This one works, in contrast to the one with the JdbcTemplate where the KeyHolder is always empty, after the insert into the database.