From: Craig Ringer <craig@postnewspapers.com.au>
To: stagirus <mamasa@stagirus.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Mapping Hibernate boolean to smallint(Postgresql)
Date: Sat, 25 Sep 2010 11:01:47 +0800
Message-ID: <4C9D661B.7010301@postnewspapers.com.au> (raw)
In-Reply-To: <1285364037415-2853280.post@n5.nabble.com>
References: <1285364037415-2853280.post@n5.nabble.com>
On 09/25/2010 05:33 AM, stagirus wrote:
> Good afternoon. I am the Chief Architect, with 19 years of experience
> in the software industry
Without meaning to sound excessively grumpy, I'm surprised that those 19
years didn't teach you to provide detailed error messages, in-depth
description, and preferably test cases with "bug" reports.
The very broad description you've provided forces everybody to guess
what you are talking about. A few excerpts of mapping code, a stack
trace, and some log messages would've made a world of difference.
> Now comes the "religious" discussion. The above design works well for
> Oracle, DB2 and MySQL, etc. But PostgresQL seems to choke. It complains
> about the data type mismatch. By reading various discussions on your forum,
> there seems to some issues with the data type mapping at the JDBC driver.
> JDBC driver does not convert boolean value (false/true) to integers (0/1).
You're correct, it doesn't. PostgreSQL has a native boolean data type.
The JDBC driver uses this type when passed a boolean value.
It might be nice for the JDBC driver to detect that the target column is
a 'short' or 'integer' column and do an implicit boolean->integer
conversion. To do that might require more database traffic (to discover
column types) though. Right now, it doesn't, so you're probably getting
this error message:
ERROR: column "y" is of type integer but expression is of type boolean
As far as I know there's no JDBC driver property to tell it to send
booleans as integers, nor is there any PostgreSQL server configuration
option that lets the server convert boolean values to integer.
Personally I'm pretty surprised that booleans aren't implicitly castable
to 'int', which would resolve the issue you're encountering.
You have a couple of options here. You can:
- Map your boolean fields as booleans in the database when using
PostgreSQL. Hibernate should do this automatically if you let it create
your DDL and it's connected to a PostgreSQL database. This is the best
option, and should be what's happening unless you're overriding
Hibernate's DDL generation.
- Change your mapped objects so the fields are shorts in java, if you
want them as shorts in the database
- Write a Hibernate UserType (this is **trivial** to do) that translates
boolean to/from database short using the setInt() and getInt() JDBC
driver methods. Then use this on your boolean mappings.
- Write a custom PostgreSQL dialect for Hibernate that ignores
PostgreSQL's boolean support and converts booleans to/from integer
--
Craig Ringer
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-bugs@postgresql.org
Cc: craig@postnewspapers.com.au, mamasa@stagirus.com
Subject: Re: Mapping Hibernate boolean to smallint(Postgresql)
In-Reply-To: <4C9D661B.7010301@postnewspapers.com.au>
References: <1285364037415-2853280.post@n5.nabble.com> <4C9D661B.7010301@postnewspapers.com.au>
* 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