Skip site navigation (1) Skip section navigation (2)

Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: stagirus <mamasa(at)stagirus(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)
Date: 2010-10-01 03:50:54
Message-ID: 4CA55A9E.80700@postnewspapers.com.au (view raw or flat)
Thread:
Lists: pgsql-bugspgsql-jdbc
On 10/01/2010 05:50 AM, stagirus wrote:
>
> Oliver: Thank you for your generous response. The options you suggested were
> not yet viable. Disagreeing is part of human nature. No worries there.
>
> But if you review Craig's remarks carefully, this issue is likely outside
> the scope of JDBC drivers. It is more to do with the Postgresql DB Engine
> that is not happy to cast or convert boolean values to SMALLINT columns.
>
> Craig.. is my interpretation of your comments right?

Yes, but that's not inherently a problem. It'd only be an issue if the 
JDBC driver was required to ensure that those conversions occurred 
correctly. As it seems it isn't require to do so, the server isn't 
obliged to handle these conversions and the JDBC driver isn't obliged to 
work around the server's restriction.

I have no strong opinion on whether Pg should accept 'short' inputs for 
'boolean' columns. I see good arguments in both directions. All I was 
saying earlier was that my casual reading of the JDBC spec (incorrectly, 
it turns out) said that jdbc drivers had to support this.

Since the JDBC spec doesn't require drivers to support this, you should 
stop relying on behaviour that isn't guaranteed by the spec.

I still don't understand why you won't fix your application code to use 
Hibernate how it's designed to be used. You're using half of Hibernate's 
database dialect and automatic type selection features (runtime SQL), 
but not the other half (DDL), so it's breaking. If you'll let Hibernate 
do it's job and produce your DDL, or stop lying to Hibernate about how 
you've defined the database by mapping the smallint fields as shorts, 
your problem will go away.

How hard is this?

@Entity
class Something {

     @Basic
     @Column
     private boolean fakeBoolean;

     public boolean getFakeBoolean() {
         return fakeBoolean != 0;
     }

     public void setFakeBoolean(boolean fakeBoolean) {
         this.fakeBoolean = (short)(fakeBoolean ? 1 : 0);
     }

}



?

Note, by the way, that Java won't let you implicitly cast between 
boolean and short. Just like Pg.



It's also easy to use a UserType to get what you want, like the one I 
wrote recently because PostgreSQL won't implicitly cast 'text' to 'xml':

http://wiki.postgresql.org/wiki/Hibernate_XML_Type



For what it's worth, I think this is partly a Hibernate/JPA issue. At 
least in JPA, there's no way to specify the SQLTYPE associated with a 
given column, which is mildly annoying, as it'd let the JDBC driver 
handle this issue.

--
Craig Ringer

In response to

Responses

pgsql-bugs by date

Next:From: Dave PageDate: 2010-10-01 07:43:32
Subject: Re: BUG #5685: Installer Error
Previous:From: Craig RingerDate: 2010-10-01 03:21:13
Subject: Re: BUG #5685: Installer Error

pgsql-jdbc by date

Next:From: Albert KuruczDate: 2010-10-01 16:14:52
Subject: Upgrade to 9 questions
Previous:From: Craig RingerDate: 2010-10-01 03:09:06
Subject: Re: [BUGS] Mapping Hibernate boolean to smallint(Postgresql)

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group