This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

PostgreSQL getKey is null

Oct 28th, 2005, 02:26 PM

I converted from MySQL to PostgreSQL. My inserts no longer work on tables with an autogenerated key.
The data gets created but the KeyHolder getKey call returns null. I am using PostgreSQL 8 and postgresql-8.1dev-402.jdbc3.jar. Anyone able to get the generated key from PostgreSQL?

How do you work with Postgres? Note that postgresql needs a sequence table to use when generating key (MySQL doesn't). Check out the postgres documentation or your framework documentation for configuration details.(HB forums for example contain tons of examples in this case).

Comment

Can you post your PostgreSQL table definitions. How are you autogenerating the key in Postgres? Last I checked Postgres did not support the auto generated key feature of JDBC, have they added that functionality?

When I run this code, I get a NullPointerException at the line that contains the return statement and a debug level log message that says, "[org.springframework.jdbc.core.JdbcTemplate] - <SQL update affected 1 rows and returned 0 keys>"

I think I know of a workaround but it involves converting this to a PostgreSQL function and using either the nextval() or currval() PostgreSQL functions to return the generated key from the function.

That sounds like a lot of work.

Please tell me that there is a way to use the KeyHolder mechanism with PostgreSQL!!!