Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5APz-0005hK-M7 for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Feb 2013 07:36:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U5APz-0003uJ-2R for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Feb 2013 07:36:43 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5APx-0003uB-Qm; Tue, 12 Feb 2013 07:36:42 +0000 Received: from outmail148096.authsmtp.net ([62.13.148.96]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5APv-0004nj-Ci; Tue, 12 Feb 2013 07:36:41 +0000 Received: from mail-c233.authsmtp.com (mail-c233.authsmtp.com [62.13.128.233]) by punt9.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id r1C7aX2X039161; Tue, 12 Feb 2013 07:36:33 GMT Received: from ayaki.localdomain (124-169-220-232.dyn.iinet.net.au [124.169.220.232]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r1C7aOrC068980 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Tue, 12 Feb 2013 07:36:29 GMT Message-ID: <5119F0F7.2030204@2ndquadrant.com> Date: Tue, 12 Feb 2013 15:36:23 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Tom Lane CC: Tom Dunstan , "Hudson, Derrick" , "Marc G. Fournier" , "pgsql-jdbc@postgresql.org" , pgsql-hackers@postgresql.org Subject: Re: [JDBC] JPA + enum == Exception References: <401084E5E73F4241A44F3C9E6FD794289CE24D03@exch-01> <28690.1360299322@sss.pgh.pa.us> In-Reply-To: <28690.1360299322@sss.pgh.pa.us> X-Enigmail-Version: 1.5.0 Content-Type: multipart/alternative; boundary="------------020902030209090909050508" X-Server-Quench: ebe15fc3-74e6-11e2-a49c-0025907707a1 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZeVg0b BQteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksacBtFaQdba1xT HGQLWldEUFp7WmV/ aAgfbAdZfEtIVhto UkhXQFFQCwdtAxke B1BcU2p1JnoEC352 bU9qXj5eXUx/cwgo EBtVEDtVZnpnbDUZ TUFZIgFJIQdDLQJF alJ6XXMKZmUAN3su JQJhdwMRFB58BR5u d0k+D344anNDPAYV D1NKEDMyGAVQEm0e PxcrMkIdG0B5 X-Authentic-SMTP: 61633235383639.1021:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 124.169.220.232/2525 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------020902030209090909050508 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 02/08/2013 12:55 PM, Tom Lane wrote: > AFAIK this is just business as usual with JDBC: setString() implies that > the parameter is of a string type. Well, it means that it's a type compatible with a java.lang.String . JDBC doesn't say much about the database-side type. It's my strong view that we should accept setString(...) for any string-like type, like xml, json, domains of text, and so on - or at least provide an easy, no-superuser-required way to tell Pg to do so. I've pushed a testcase to: https://github.com/ringerc/scrapcode/tree/master/testcases/postgresql/jdbc that you can run if you want to see/experiment with the JDBC behaviour. I'll attach a tgz of it to a follow-up mail. Notably, you'll see that setObject(position, "value") is insufficient; you need to explicitly invoke setObject with Types.OTHER. > It'll fall over if the type actually > required is anything but a string. (I'm no Java expert, but I seem to > recall that using setObject instead is the standard workaround.) Unfortunately, setObject(...) isn't a complete workaround; it doesn't know about PostgreSQL-specific types, and setObject(1, "value") won't work, you have to explicitly specify Types.OTHER, eg setObject(1, "{\"key\" : \"value\"}", Types.OTHER); More importantly, for many Java users there are layers on top of JDBC that expect the database to be moderately lenient about accepting java.lang.String arguments. The extreme strictness Pg imposes makes things hard because most of these tools don't provide ways to punch through the abstraction and specify database types - they haven't needed to, because other DBs let you implicitly cast to/from things that look like strings. PgJDBC could work around this by treating setString(...) and the two-argument setObject(...) as if they were setObject(..., Types.Other). However, this would risk breaking queries that currently rely on the explicit text type to resolve functions that would otherwise be ambiguous. The same issue may apply to making conversions for text-like types more lenient in the server, and it wouldn't help people who want to: @Entity public class MyJPAEntity { //.... @Column private String jsonField; } ... in their JPA mappings backed by Hibernate/EclipseLink/etc. They really need a session-level, DB-level or user-level (ie: GUC) way to say "Let me cast implicitly between json/xml/etc and text". -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------020902030209090909050508 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 02/08/2013 12:55 PM, Tom Lane wrote:
AFAIK this is just business as usual with JDBC: setString() implies that
the parameter is of a string type.
Well, it means that it's a type compatible with a java.lang.String . JDBC doesn't say much about the database-side type.

It's my strong view that we should accept setString(...) for any string-like type, like xml, json, domains of text, and so on - or at least provide an easy, no-superuser-required way to tell Pg to do so.

I've pushed a testcase to:

    https://github.com/ringerc/scrapcode/tree/master/testcases/postgresql/jdbc

that you can run if you want to see/experiment with the JDBC behaviour. I'll attach a tgz of it to a follow-up mail.

Notably, you'll see that setObject(position, "value") is insufficient; you need to explicitly invoke setObject with Types.OTHER.

  It'll fall over if the type actually
required is anything but a string.  (I'm no Java expert, but I seem to
recall that using setObject instead is the standard workaround.)
Unfortunately, setObject(...) isn't a complete workaround; it doesn't know about PostgreSQL-specific types, and setObject(1, "value") won't work, you have to explicitly specify Types.OTHER, eg setObject(1, "{\"key\" : \"value\"}", Types.OTHER);

More importantly, for many Java users there are layers on top of JDBC that expect the database to be moderately lenient about accepting java.lang.String arguments. The extreme strictness Pg imposes makes things hard because most of these tools don't provide ways to punch through the abstraction and specify database types - they haven't needed to, because other DBs let you implicitly cast to/from things that look like strings.

PgJDBC could work around this by treating setString(...) and the two-argument setObject(...) as if they were setObject(..., Types.Other). However, this would risk breaking queries that currently rely on the explicit text type to resolve functions that would otherwise be ambiguous. The same issue may apply to making conversions for text-like types more lenient in the server, and it wouldn't help people who want to:

@Entity
public class MyJPAEntity {
    //....

    @Column
    private String jsonField;

}

... in their JPA mappings backed by Hibernate/EclipseLink/etc. They really need a session-level, DB-level or user-level (ie: GUC) way to say "Let me cast implicitly between json/xml/etc and text".
-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services
--------------020902030209090909050508--