Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 88EA11337BA1 for ; Thu, 30 Sep 2010 17:38:45 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 08258-02 for ; Thu, 30 Sep 2010 20:38:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from tremor.opencloud.co.nz (tremor.opencloud.co.nz [131.203.101.230]) by mail.postgresql.org (Postfix) with ESMTP id A58B31337B7D for ; Thu, 30 Sep 2010 17:38:31 -0300 (ADT) Received: from 121-98-80-66.bitstream.orcon.net.nz ([121.98.80.66] helo=[192.168.1.2]) by tremor.opencloud.co.nz with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1P1PtW-0000XS-Qj; Fri, 01 Oct 2010 09:38:23 +1300 Message-ID: <4CA4F536.5050704@opencloud.com> Date: Fri, 01 Oct 2010 09:38:14 +1300 From: Oliver Jowett User-Agent: Thunderbird 2.0.0.24 (X11/20100411) MIME-Version: 1.0 To: Craig Ringer CC: stagirus , pgsql-jdbc@postgresql.org Subject: Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql) References: <1285601721239-2855475.post@n5.nabble.com> <4CA0805B0200002500035F43@gw.wicourts.gov> <4CA101E3.5080005@opencloud.com> <4CA1743F.2060301@postnewspapers.com.au> <4CA18729.90401@postnewspapers.com.au> <1285701298407-2865634.post@n5.nabble.com> <4CA22C3C0200002500036065@gw.wicourts.gov> <1285716712781-2916249.post@n5.nabble.com> <4CA28CCB.5020400@opencloud.com> <1285762010765-3005405.post@n5.nabble.com> <4CA338F8.80902@opencloud.com> <4CA499DB.8000807@postnewspapers.com.au> In-Reply-To: <4CA499DB.8000807@postnewspapers.com.au> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201009/92 X-Sequence-Number: 20962 Craig Ringer wrote: > It looks like "stagirus" may be right about the JDBC spec requiring > conversion between all numeric types, and considering "boolean" to be a > numeric special case. Page 217 of the JDBC4 spec (see > http://java.sun.com/products/jdbc/download.html#corespec40) contains a > table that suggests that setting a Java 'boolean' to a database > 'smallint' (and vice versa) should be valid and permitted. This is not correct. I covered this in a previous mail. This table (table B-5) is specifically about the conversions that setObject() will do on require. See page 192 at the start of appendix B: > TABLE B-5 Conversions by setObject and setNull from Java Object Types to JDBC > Types > This table shows which JDBC types may be specified as the target JDBC type to the > methods PreparedStatement.setObject, PreparedStatement.setNull, RowSet.setNull > and RowSet.setObject. Section 13.2.2.1, page 100: > The data type specified in a PreparedStatement setter method is a data type in > the Java programming language. The JDBC driver is responsible for mapping this to > the corresponding JDBC type (one of the SQL types defined in java.sql.Types) so > that it is the appropriate type to be sent to the data source. The default mapping is > specified in Appendix B TABLE B-2. (Table B-2 specifies that a Java boolean is mapped to JDBC datatypes BIT or BOOLEAN) And section 13.2.2.3, page 101: > The method setObject can be used to convert an object in the Java programming > language to a JDBC type. > The conversion is explicit when setObject is passed a Java Object and a JDBC > data type. [...] > If setObject is called without a type parameter, the Java Object is implicitly > mapped using the default mapping for that object type. [...] > The default mapping is described in Appendix B TABLE B-4 (Table B-4 specifies that a Java boolean is mapped to JDBC datatypes BIT or BOOLEAN) So, in summary: If you explicitly ask for a Java boolean to be mapped to a JDBC SMALLINT, by passing Types.SMALLINT to setObject(), then that should be supported and the driver will do the conversion for you; if that doesn't work, that's a bug! However, if you don't specify a type when calling setObject() or setBoolean(), it is mapped to BOOLEAN. If BOOLEAN is then not a suitable type for the actual query you're trying to run, then you'll get a SQL error, but that's not the driver's fault, it just did what you asked! So the driver is working just fine here - the problem is that the application (via Hibernate) is expecting that a BOOLEAN value should be implicitly cast to a SMALLINT, which is really nonportable behaviour which only happens to work with Oracle because Oracle doesn't have a SQL boolean type in the first place and so the app is using SMALLINT in its DDL .. -O