Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9654C1337B50 for ; Tue, 26 Oct 2010 11:37:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 93148-04 for ; Tue, 26 Oct 2010 14:36:58 +0000 (UTC) X-Greylist: delayed 00:08:41.285854 by SQLgrey-1.7.6 Received: from mail0.unitn.it (mail0.unitn.it [193.205.206.51]) by mail.postgresql.org (Postfix) with ESMTP id D105B1336DB6 for ; Tue, 26 Oct 2010 11:36:55 -0300 (ADT) Received: from mail0.unitn.it (localhost.localdomain [127.0.0.1]) by localhost (Postfix) with SMTP id 784BF4498D for ; Tue, 26 Oct 2010 16:28:11 +0200 (CEST) Received: from mailhub1.unitn.it (mailhub.unitn.it [192.168.206.46]) by mail0.unitn.it (Postfix) with ESMTP id 3D9C644534 for ; Tue, 26 Oct 2010 16:28:11 +0200 (CEST) Received: from disi.unitn.it (dit.unitn.it [193.205.194.4]) by mailhub1.unitn.it (Postfix) with ESMTP id 8648CAE5466 for ; Tue, 26 Oct 2010 16:28:10 +0200 (CEST) Received: from pravdinPC ([192.168.187.157]) by disi.unitn.it (8.13.8/8.13.8) with ESMTP id o9QESATf010782 for ; Tue, 26 Oct 2010 16:28:10 +0200 From: "Viktor Pravdin" To: Subject: getGeneratedKeys() problem Date: Tue, 26 Oct 2010 16:19:26 +0200 Message-ID: <004801cb7518$cbc632e0$635298a0$@pravdin@disi.unitn.it> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Act1GMtqX0njtVEhRb6moMZn5kz0lA== Content-Language: ru X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-10 required=5 tests=BAYES_00=-1.9, MSGID_MULTIPLE_AT=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/150 X-Sequence-Number: 21118 Hello, I've encountered a strange problem with the Statement.getGeneratedKeys() method. In our project we use Hibernate with PostgreSQL, the issue was detected with Hibernate 3.5.0-Final and 3.6.0.Final, PostgreSQL server 9.0.0-1 and PostgreSQL JDBC driver 9.0-801.jdbc3/4 on Windows 7 32 bit. The issue is that for some reason the call to PreparedStatement.getGeneratedKeys() returns all columns of the table and not just the one that has the generated key, and since Hibernate expects the identity column in case of the long IDs to be the first one in the list and the schema is generated automatically and thus the column order is undefined, we got an org.postgresql.util.PSQLException: Bad value for type long: . In other words, Hibernate expects getGeneratedKeys() to return only the column which contains the long ID, but instead the driver returns all of them, and if the first one isn't the ID column then Hibernate crashes. If I understand correctly the prepared statement is generated by AbstractJdbc3Connection.prepareStatement (String sql, int autoGeneratedKeys) which results in a query like "INSERT blah-blah RETURNING *". Could it be the case that this "RETURNING *" statement is what actually causes all columns to be returned by getGeneratedKeys()? Here's a really simple test case: CREATE TABLE users ( id bigserial NOT NULL, creation_date timestamp without time zone NOT NULL, modification_date timestamp without time zone NOT NULL, email character varying(255) NOT NULL, "login" character varying(255) NOT NULL, "password" character varying(255) NOT NULL ) public static void main(String[] args) throws SQLException { Connection conn=DriverManager.getConnection(dbUrl); PreparedStatement ps=conn.prepareStatement("INSERT INTO USERS (creation_date,modification_date,email,\"login\",\"password\") VALUES (?,?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS); ps.setDate(1, new Date(System.currentTimeMillis())); ps.setDate(2,new Date(System.currentTimeMillis())); ps.setString(3, "a@b.c.d"); ps.setString(4,"testjdbc"); ps.setString(5,"test"); ps.executeUpdate(); System.out.println("Column count: "+ps.getGeneratedKeys().getMetaData().getColumnCount()); } It returns 6 columns instead of 1. So, let me sum it up: in the case of PreparedStatement ps = connection.prepareStatement( sql, PreparedStatement.RETURN_GENERATED_KEYS ); ps.executeUpdate(); ResultSet rs = rs.getGeneratedKeys(); the result set contains all columns of the inserted record instead of containing the ID column(s) only. Is it a bug in the JDBC driver or is there something wrong in the way the statement is prepared? How could it be resolved in the latter case? Thanks, Viktor Pravdin