pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-jdbc@postgresql.org, stagirus <mamasa@stagirus.com>
Subject: Re: [BUGS] Mapping Hibernate boolean to	 smallint(Postgresql)
Date: Tue, 28 Sep 2010 09:43:15 +1300
Message-ID: <4CA101E3.5080005@opencloud.com> (raw)
In-Reply-To: <4CA0805B0200002500035F43@gw.wicourts.gov>
References: <4CA064B50200002500035F29@gw.wicourts.gov>
	<1285601721239-2855475.post@n5.nabble.com>
	<4CA0805B0200002500035F43@gw.wicourts.gov>

Kevin Grittner wrote:

> The relevant portions are TABLE B-5 and TABLE B-6. [...]

> But getBoolean against a SMALLINT is
> clearly supposed to be attempted on demand.  I didn't happen across
> the specification of how values map there, but in the absence of
> evidence to the contrary I'd assume zero is false and anything else
> is true;  Likewise, setObject using a Boolean against a SMALLINT
> target is supposed to work.

If I read the original report right, it's about setBoolean(), not
getBoolean() (the error is a type mismatch while executing the query,
not a problem processing the results)

Note that table B-5 is specifically about "What combinations of
java.sql.Types value and actual instance type are valid for
setObject()?". So, for example, if you called "setObject(column,
Boolean.TRUE, Types.SMALLINT)", that should work - you're explicitly
asking the driver to represent a Boolean as a SMALLINT. (As noted, the
mapping's not standard, so you may not get exactly the same as other
databases).

However, if you just call setBoolean() in a context where the database
is expecting a SMALLINT, then it's not going to work - how does the
driver know, in the general case, that it should apply a conversion
there? (Consider fun cases where the parameter isn't just directly
mapped to a column, it's part of an expression, etc).

The driver could in theory ask the backend to always infer a type for
positional parameters, then apply its own conversions, but this requires
an extra round trip per query and would actually break other cases where
valid queries with correct types would suddenly start complaining about
being unable to infer a type. And anyway, it's not the right place for
it - the backend already has a large set of type conversion
functionality, why are we suddenly trying to duplicate that in the driver?

You can think of setBoolean(column, boolValue) as essentially the same
as setObject(column, Boolean.valueOf(boolValue), Types.BOOLEAN) - if you
look at it that way, there's obviously no conversion required, because
the type you passed is already a BOOLEAN as you requested ..

I would suggest that the OP either:

* use a real boolean column in their schema (I mean, the data you're
representing IS a boolean value in this case, right?); or
* use setObject(column, value, Types.SMALLINT) instead of the implied
setBoolean() call when setting the column (this may be tricky to do
since you have a layer between you and the driver, but that's not really
the driver's fault..); or
* add a suitable implicit cast on the backend side from boolean ->
smallint (not sure if this works, haven't looked at the details)

-O



view thread (53+ messages)  latest in thread

Message-ID: <4CA101E3.5080005@opencloud.com>
Permalink:  ../4CA101E3.5080005@opencloud.com/
Also on:    postgresql.org/message-id/4CA101E3.5080005@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, Kevin.Grittner@wicourts.gov, mamasa@stagirus.com
  Subject: Re: [BUGS] Mapping Hibernate boolean to	 smallint(Postgresql)
  In-Reply-To: <4CA101E3.5080005@opencloud.com>
  References: <4CA064B50200002500035F29@gw.wicourts.gov> <1285601721239-2855475.post@n5.nabble.com> <4CA0805B0200002500035F43@gw.wicourts.gov> <4CA101E3.5080005@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox