我在这个标题中找到的其他问题都涉及非SELECT查询。我正在使用Java 8,Sql2o 1.5.4和postgresql 9.5.3。
我的UserService看起来像:
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;
}
}
我的用户看起来像:
public class User {
private int id;
private String firstname;
private String lastname;
private String email;
private String passhash;
private int teamId;
/*getters and setters*/
}
我的测试看起来像:
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");
}
}
当我直接对数据库执行SQL时,我得到了预期的记录,在这种情况下team_id为NULL。
当我运行测试时,我得到以下异常:
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)
为什么会这样?我该如何解决?我的PGService测试正在传递,它是从DBCP2 BasicDataSource创建的。如果您需要更多详细信息,请与我们联系。
答案 0 :(得分:1)
问题不在UserService中,而是在我的PGService类中。这是:
public class PGService {
private final Sql2o connectionPool;
public PGService() throws ConfigurationException, IOException {
Config cfg = loadConfig("dbconfig.json");
if (cfg == null) {
throw new ConfigurationException("Could not load dbconfig.");
}
BasicDataSource bds = new BasicDataSource();
bds.setUsername(cfg.getUsername());
bds.setPassword(cfg.getPassword());
bds.setDriverClassName("org.postgresql.Driver");
bds.setUrl(cfg.getUrl());
bds.setInitialSize(1);
connectionPool = new Sql2o(bds);
}
public Connection getConnection() {
return this.connectionPool.open();
}
}
下面的修复和解释,来自Sql2o Google小组修复了我的问题,与使用postgres时RETURNING语法的错误紧密相关。
“RETURNING或其附近的语法错误”是由a引起的 sql2o自动处理的方式不兼容 生成的数据库和postgres jdbc驱动程序中的键。什么时候 使用postgres,当sql2o检查是否存在异常时抛出异常 生成数据库中的任何键。解决方案是永远不要检查 键,除非您明确指望生成密钥。
这是由PostgresQuirks类在sql2o中处理的。所以,什么时候 创建你的Sql2o实例,使用其中一个构造函数重载 将Quriks实例作为参数:
Sql2o sql2o = new Sql2o(bds,new PostgresQuirks());
应该修复它!并确保删除后的' - ' 查询。
无论如何,我希望这有助于将来遇到此问题的任何人。
干杯。