I have two tables that I am trying to insert a row to. Connected by a foreign key. I am using java 8 postgres 9.5.4.
I have tried using the RETURNING clause which works while in pgAdmin (values >= 1) but when run in using my java app. the value is then set to -1 and an error is thrown.
This is my working query for pgAdmin,
INSERT INTO counting_table (count1, cout2)
VALUES (3, 2) RETURNING id
this returns the proper value in pgAdmin, but when entered as,
@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);
}
}
When the line
int id = personDao.insertCountingTable(count1, count2);
is executed the id is set to -1 but the value should be >= 1, also checking the database I see that the row has been successfully inserted.
I could not get the RETURNING
clause to work the way that I wanted to but using the @GetGeneratedKeys
annotation above my query and removing the RETURNING
line, I was able to achieve the desired results.
My code now looks as such,
@SqlUpdate("INSERT INTO counting_table (count1, count2) " +
"VALUES (:count1, :count2)")
@GetGeneratedKeys
int insertCountingTable(@Bind("count1") int count1,
@Bind("count2")int count2);