0

The following code produces an error for me in Eclipse Luna and prevents from running.

    jdbcTemplate.query( query, r -> {
        Folder folder = new Folder();
        folder.setId(r.getLong("id"));
        folder.setFolderName(r.getString("folder_name"));
        folder.setFullPath(r.getString("full_path"));
        folders.add(folder);
    }, folderId);

    return folders;
}

The error occurs at jdbcTemplate.query

The method query(String, ResultSetExtractor<Object>, Object[]) is ambiguous for the type JdbcTemplate

Error does not occur when I package file as jar and run.

Thanks for any guidance or suggestions.

1
  • your lambda expression needs to return an object at the end of it's body Commented May 1, 2016 at 22:36

2 Answers 2

3

There are three query methods in JdbcTemplate, as follows:

query(String, ResultSetExtractor<T>);
query(String, RowCallbackHandler);
query(String, RowMapper<T>);

In all three, the second argument can be implemented as a lambda, and the two that have a generic <T> parameter will return something. Eclipse is complaining that it can't tell the difference between the first and the third method you are referring to, so you need to specify the type of the functional interface that the lambda is replacing. Additionally, as Jack Ammo says, you need to return an object from the lambda to satisfy the interface. Don't add the returned object to your list of folders inside the lambda, either. So, you'd be looking at something like this:

Folder folder = jdbcTemplate.query( query, (ResultSetExtractor<Folder>) r -> {
    Folder folder = new Folder();
    folder.setId(r.getLong("id"));
    folder.setFolderName(r.getString("folder_name"));
    folder.setFullPath(r.getString("full_path"));
    return folder;
}, folderId);

folders.add(folder);

return folders;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Casting as RowCallbackHandler solved problem.
2

according to the javadoc for ResultSetExtractor, the method you're implementing with your lambda expression is Object extractData(ResultSet rs). extractData returns an Object so your lambda also has to return an Object in order to properly implement it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.