0
The thing is, I’m a beginner in Spring and I’m doing a method that will insert a new row into a database table and this method will return the Id
new that was generated by the Bank, I am doing this way:
public Profissional insert(Profissional profissional) {
PreparedStatementCreator psc = connection -> {
PreparedStatement ps = connection.prepareStatement("insert into profissional(texto) values(?)",
new String[] { "id" });
ps.setString(1, profissional.getDsNomeProfissional());
return ps;
};
KeyHolder keyHolder = new GeneratedKeyHolder();
this.jdbc.update(psc, keyHolder);
profissional.setIdProfissional(keyHolder.getKey().longValue());
return profissional;
}
and is working and returning the id
generated by the bank, but on this line profissional.setIdProfissional(keyHolder.getKey().longValue());
to IDE
you’re warning me that keyHolder.getKey().longValue()
can return null, and I am worried about it but also with doubt Docs of the Spring You’re saying this is the right way, is it even possible? If yes, can someone explain to me if there is any other way to make this code without danger of returning one Id
null?
(Note: this is a more informative question in case someone else has this same doubt that I.)
Thank you very much.
– Bruno