KeyHolder.getKey () may return null - Spring Boot JdbcTemplate

0
I'm starting Spring and I'm doing a method that will insert a new row into a database table and this method will return the% new% that was generated by the Bank, I'm doing this:

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 in this line id to profissional.setIdProfissional(keyHolder.getKey().longValue()); is warning me that IDE can return null, and I'm worried about this but also with doubts because Spring Docs is saying that this is the right way, Is this really possible? If yes, can someone explain to me if there is any other way to do this code without danger of returning% n of% null?

(Note: this is a more informative question if someone else has this same doubt as me.)

    
asked by anonymous 06.10.2018 / 00:16

0 answers