Java Lambda Loopback

I have a problem with Java Lambda Expressions. I am using Spring 4, JdbcTemplate, Java 8. Intellij IDEA shows "Loopback". What is it and how to fix it? Thank you for the attention.

@Override
public User getUser(long id) {
    return jdbcTemplate.query("SELECT * FROM user WHERE id = ?",
            ps -> {
                ps.setLong(1, id);
            },
            (rs, rowNum) -> {
                return new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getLong(4), rs.getBoolean(5));
            });
}

enter image description here

solvable This function returns a list, not a user. And the correct function is this:

@Override
public User getUser(long id) {
    return jdbcTemplate.query("SELECT * FROM user WHERE id = ?",
            ps -> {
                ps.setLong(1, id);
            },
            (rs, rowNum) -> {
                return new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getLong(4), rs.getBoolean(5));
            }).get(0);
}
+4
source share
1 answer
@Override
public User getUser(long id) {
    return jdbcTemplate.query("SELECT * FROM user WHERE id = ?",
            ps -> {
                ps.setLong(1, id);
            },
            (rs, rowNum) -> {
                return new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getLong(4), rs.getBoolean(5));
            }).stream().findFirst().orElse(null);
}

Or you can also use .findAny instead of findFirst.

0
source

All Articles