RETURNING 子句在 pgAdmin 中有效,但 returns -1 对于我的 Java 代码
RETURNING clauses works in pgAdmin but returns -1 for my Java code
我有两个表要插入一行。通过外键连接。我正在使用 java 8 postgres 9.5.4。
我尝试使用 RETURNING 子句,该子句在 pgAdmin 中有效(值 >= 1),但在 运行 使用我的 java 应用程序时有效。然后将该值设置为 -1 并抛出错误。
这是我对 pgAdmin 的工作查询,
INSERT INTO counting_table (count1, cout2)
VALUES (3, 2) RETURNING id
这 returns pgAdmin 中的正确值,但是当输入时,
@SqlUpdate("INSERT INTO counting_table (count1, count2) " +
"VALUES (:count1, :count2) " +
"RETURNING id")
int insertCountingTable(@Bind("count1") int count1,
@Bind("count2")int count2);
@CreateSqlObject
@Override
public void add(Person person, int count1, int count2)
{
try(Handle handle = jdbi.open())
{
PersonDao personDao = handle.attach(PersonDao.class);
int id = personDao.insertCountingTable(count1, count2);
personDao.insertPerson(person.getPersonId(),
person.getName(), id);
}
}
当行
int id = personDao.insertCountingTable(count1, count2);
被执行id设置为-1但是值应该>=1,
还检查数据库我看到该行已成功插入。
我无法让 RETURNING
子句按照我想要的方式工作,但是使用查询上方的 @GetGeneratedKeys
注释并删除 RETURNING
行,我能够达到预期的效果。
我的代码现在看起来是这样的,
@SqlUpdate("INSERT INTO counting_table (count1, count2) " +
"VALUES (:count1, :count2)")
@GetGeneratedKeys
int insertCountingTable(@Bind("count1") int count1,
@Bind("count2")int count2);
我有两个表要插入一行。通过外键连接。我正在使用 java 8 postgres 9.5.4。
我尝试使用 RETURNING 子句,该子句在 pgAdmin 中有效(值 >= 1),但在 运行 使用我的 java 应用程序时有效。然后将该值设置为 -1 并抛出错误。
这是我对 pgAdmin 的工作查询,
INSERT INTO counting_table (count1, cout2)
VALUES (3, 2) RETURNING id
这 returns pgAdmin 中的正确值,但是当输入时,
@SqlUpdate("INSERT INTO counting_table (count1, count2) " +
"VALUES (:count1, :count2) " +
"RETURNING id")
int insertCountingTable(@Bind("count1") int count1,
@Bind("count2")int count2);
@CreateSqlObject
@Override
public void add(Person person, int count1, int count2)
{
try(Handle handle = jdbi.open())
{
PersonDao personDao = handle.attach(PersonDao.class);
int id = personDao.insertCountingTable(count1, count2);
personDao.insertPerson(person.getPersonId(),
person.getName(), id);
}
}
当行
int id = personDao.insertCountingTable(count1, count2);
被执行id设置为-1但是值应该>=1, 还检查数据库我看到该行已成功插入。
我无法让 RETURNING
子句按照我想要的方式工作,但是使用查询上方的 @GetGeneratedKeys
注释并删除 RETURNING
行,我能够达到预期的效果。
我的代码现在看起来是这样的,
@SqlUpdate("INSERT INTO counting_table (count1, count2) " +
"VALUES (:count1, :count2)")
@GetGeneratedKeys
int insertCountingTable(@Bind("count1") int count1,
@Bind("count2")int count2);