The other questions I've found with this title all deal with non-SELECT queries. I'm using Java 8, Sql2o 1.5.4, and postgresql 9.5.3.
My UserService looks like:
public class UserService {
private final PGService pgService;
public UserService(PGService _pgs) {
this.pgService = _pgs;
}
public User getUserById(int id) {
String sql = "SELECT id, firstname, lastname, email, team_id teamId FROM users WHERE id = :id;--";
User user;
try (Connection c = pgService.getConnection()) {
user = c.createQuery(sql)
.addParameter("id", id)
.executeAndFetchFirst(User.class);
}
return user;
}
}
My user looks like:
public class User {
private int id;
private String firstname;
private String lastname;
private String email;
private String passhash;
private int teamId;
/*getters and setters*/
}
My test looks like:
public class UserServiceTest {
private static UserService service;
@Before
public void setUp() throws ConfigurationException, IOException {
this.service = new UserService(new PGService());
}
@Test
public void returnsBiffUser() {
User biff = service.getUserById(3);
assertTrue(biff != null && biff.getLastname() == "Biff");
}
}
When I execute the SQL directly against the database I get the expected record, and team_id is NULL in this case.
When I run the test, I get the following exception:
org.sql2o.Sql2oException: Database error: No results were returned by the query.
at org.sql2o.Query$ResultSetIterableBase.<init>(Query.java:332)
at org.sql2o.Query$10.<init>(Query.java:412)
at org.sql2o.Query.executeAndFetchLazy(Query.java:412)
at org.sql2o.Query.executeAndFetchFirst(Query.java:480)
at org.sql2o.Query.executeAndFetchFirst(Query.java:469)
at services.UserService.getUserById(UserService.java:24)
at services.UserServiceTest.returnsBiffUser(UserServiceTest.java:25)
Caused by: org.postgresql.util.PSQLException: No results were returned by the query.
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:115)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
at org.sql2o.Query$ResultSetIterableBase.<init>(Query.java:328)
Why is this happening? How can I fix it? My PGService tests are passing and it is created from a DBCP2 BasicDataSource. Please let me know if you need more detail.
The issue was not in the UserService, it was in my PGService class. Which was:
The fix and explanation below, from the Sql2o Google group fixed my problem and is tightly related to the error surrounding RETURNING syntax when using postgres.
Anyways, I hope this helps anyone who encounters this issue in the future.
Cheers.