From: Craig Ringer <craig@postnewspapers.com.au>
To: Samuel Gendler <sgendler@ideasculptor.com>
Cc: Oliver Jowett <oliver@opencloud.com>
Cc: 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 14:11:53 +0800
Message-ID: <4CA18729.90401@postnewspapers.com.au> (raw)
In-Reply-To: <4CA1743F.2060301@postnewspapers.com.au>
References: <4CA064B50200002500035F29@gw.wicourts.gov>
<1285601721239-2855475.post@n5.nabble.com>
<4CA0805B0200002500035F43@gw.wicourts.gov>
<4CA101E3.5080005@opencloud.com>
<AANLkTi=OgduBWb4XLo3bqUXJULNEvUKCAh54xhiEgOjU@mail.gmail.com>
<4CA1743F.2060301@postnewspapers.com.au>
I got sick of the guesswork and hand-waving, so here's a test case
demonstrating that Hibernate works just fine with PostgreSQL when
mapping booleans as shorts or as booleans.
Executable:
http://www.postnewspapers.com.au/~craig/weblinks/pghiberboolean-1.0-SNAPSHOT-jar-with-dependencies.j...
Sources including build README and Maven pom:
http://www.postnewspapers.com.au/~craig/weblinks/pghiberboolean-1.0-SNAPSHOT-project.zip
Run the test case as:
java -jar target/pghiberboolean-1.0-SNAPSHOT-jar-with-dependencies.jar \
jdbc:postgresql://localhost/DBNAME DBUSER DBPASSWORD
(all on one line, replace DBNAME, DBUSER, DBPASSWORD with settings for a
test database on your machine).
Now: please show your code so we can see what's actualling going wrong,
as this case demonstrates that Hibernate gets it right when left to its
own devices.
Are you using your own DDL to define your tables, so you're mapping
types as shorts in hibernate but using booleans in the database? Because
that's not how Hibernate expects it to be, it expects a short if you
tell it you're mapping a short.
I'm going to guess that what you're doing is equivalent to running the
attached test program to create the tables, then connecting to the
database and running:
ALTER TABLE test_entity DROP COLUMN not_boolean;
ALTER TABLE test_entity ADD COLUMN not_boolean boolean;
thus changing the "shortint" typed column to "boolean". This will cause
the test program to fail with:
SEVERE: ERROR: column "not_boolean" is of type boolean but expression is
of type smallint
Hint: You will need to rewrite or cast the expression.
Position: 67
If this is the case, you can:
- Let hibernate generate the DDL in a database-independent manner;
- Fix your SQL DDL so it declares booleans where Hibernate expects
booleans;
- Fix your mappings so they're Boolean in java if they're boolean in
the database, and let Hibernate take care of mapping them to shorts
when the database doesn't understand booleans natively;
- Write a UserType to hack around this by using the short-typed get and
set methods in JDBC rather than the boolean ones, overriding
Hibernate's defaults.
Basically, I think you're throwing a spanner in the works by trying to
override Hibernate, but only doing it half-way.
--
Craig Ringer
Tech-related writing: http://soapyfrogs.blogspot.com/
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: craig@postnewspapers.com.au, sgendler@ideasculptor.com, oliver@opencloud.com, Kevin.Grittner@wicourts.gov, mamasa@stagirus.com
Subject: Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)
In-Reply-To: <4CA18729.90401@postnewspapers.com.au>
References: <4CA064B50200002500035F29@gw.wicourts.gov> <1285601721239-2855475.post@n5.nabble.com> <4CA0805B0200002500035F43@gw.wicourts.gov> <4CA101E3.5080005@opencloud.com> <AANLkTi=OgduBWb4XLo3bqUXJULNEvUKCAh54xhiEgOjU@mail.gmail.com> <4CA1743F.2060301@postnewspapers.com.au> <4CA18729.90401@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