Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id CA7201336B42 for ; Tue, 8 Feb 2011 07:34:27 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 19460-09 for ; Tue, 8 Feb 2011 11:34:20 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from prod2.officenet.no (unknown [195.159.87.248]) by mail.postgresql.org (Postfix) with ESMTP id CBE08133616B for ; Tue, 8 Feb 2011 07:34:19 -0400 (AST) Received: from localhost ([127.0.0.1] helo=prod2) by prod2.officenet.no with esmtp (Exim 4.71) (envelope-from ) id 1Pmlpr-0004t8-J4 for pgsql-jdbc@postgresql.org; Tue, 08 Feb 2011 12:34:19 +0100 Date: Tue, 8 Feb 2011 12:34:19 +0100 (CET) From: Andreas Joseph Krogh To: pgsql-jdbc@postgresql.org Message-ID: <23828143.70.1297164859086.JavaMail.on@prod2> In-Reply-To: <4D511C79.4010507@opencloud.com> Subject: Re: ResultSet.getClob() causing problems when used with JPA's @Lob MIME-Version: 1.0 User-Agent: Origo 1.8.9-SNAPSHOT X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.107 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Archive-Number: 201102/42 X-Sequence-Number: 21440 P=C3=A5 tirsdag 08. februar 2011 kl 11:35:37 skrev du: > Andreas Joseph Krogh wrote: >=20 > > Firstly; My issue here only affects CLOBs, not BLOBs. >=20 > BLOB-vs-bytea is essentially the same issue as CLOB-vs-varchar, FWIW. If= =20 > you tried to use the BLOB interface on a bytea column or vice versa,=20 > you'd see similar problems, because bytea is not a BLOB, regardless of=20 > the fact that they're both ways of storing large binary data. >=20 > > Thirdly; Didn't we just agree on that PG doesn't have CLOBs? If PG does= n't have CLOBs, how can it map CLOBs to OIDs? Under what circumstances woul= d PG need an OID to map a *C*LOB? Are you suggesting to store CLOBs as byte= a? Is anyone doing this? >=20 > No, I don't think we agreed on that. I meant on the server-side. > The PG driver certainly does have=20 > something that is a CLOB - it is a column that contains an OID=20 > referencing a separate LO that contains character data. The server=20 > itself doesn't know about that mapping, but when you're working at the=20 > JDBC level, you do need to be aware of the mapping. >=20 > (One way to look at it is to pretend that "clob" is spelled "oid" in=20 > your schema) And what benefits does having a LO referencing character-data gives you ins= tead of just using varchar/text? I don't see any motivation for using a LO? > > All this stuff sounds like an implementation detail to me. The programm= er using JDBC should, IMO, be able to use rs.setClob() to varchar/text-colu= mns transparently as there's no reason to treat it differently than setStri= ng(). >=20 > Why do you assume this? JDBC's CLOB is not the same type as VARCHAR, and= =20 > JDBC doesn't require that they're interchangeable. >=20 > And, in fact, the postgresql driver's mapping of CLOB is not=20 > interchangeable with VARCHAR, which is the root of your problem.. >=20 > >> The fundamental question here is "why are you trying to map a varchar = to > >> a Clob?" .. As you say, clobs are mostly redundant anyway. Given that > >> your schema uses varchar, why don't you just use the varchar-style > >> accessors? It's not unreasonable to expect your access method to match > >> the underlying schema, surely. > >=20 > > The reason I map the String-property as @Lob (which, by default, makes = Hibernate use setClob()) is for interoperability with other DBs, like Oracl= e, which require this annotation in order to be able to store Strings which= are over 4000 chars long. These properties are stored in Oracle as CLOB an= d in PG as VARCHAR. >=20 > Your problem here is that by using the JDBC driver's set/getClob()=20 > methods (indirectly via Hibernate), you are saying "I want to treat this= =20 > type as a CLOB"; and a CLOB is mapped by the postgresql driver to the=20 > *oid* type, *not* the varchar type you are using in your schema. >=20 > So you have a mismatch between your schema and how you are trying to=20 > access it. Is there some reason your schema can't use oid here, as the=20 > driver requires? >=20 > When you're using something like Hibernate you have three layers involved: >=20 > 1) your application layer, which is dealing in terms of Java objects > 2) your persistence layer, which handles the mapping of the Java objects= =20 > to a SQL schema, perhaps in a database-specific or schema-specific way=20 > (as customized by annotations etc) > 3) the JDBC layer, which handles the database-specific parts of taking a= =20 > SQL query expressed via the JDBC API and giving it to the database. >=20 > The JDBC layer (3) really is quite a simple mapping. It does not have=20 > any knowledge of how you want to handle the data up at layer (1) - it=20 > just does specifically what is asked of it. When Hibernate calls, e.g.,= =20 > setClob() or getClob() it is explicitly asking the driver to interpret=20 > that parameter/column as a JDBC CLOB, which in the postgresql world=20 > means "a LO containing character data referenced by OID". If that=20 > interpretation doesn't match your schema, that's not a problem with the= =20 > driver - it means that layer (2) doesn't have the right mapping set up.= =20 > Putting knowledge in layer (3) about how to map your particular schema=20 > to the particular datatypes you want is really the wrong place for it -= =20 > the JDBC API just doesn't provide a place to put that information. >=20 > (TBH, I'd think the simplest solution would be to just teach Hibernate's= =20 > postgresql dialect to map String-with-@Lob to the text type - the clob=20 > support in the driver is limited at best) I hear over and over that PGs JDBC-driver wants to map CLOBs to OIDs but th= ere really doesn't seem to be any good reasons for it (at least not that I = can see). Why would I want to map my data as OID instead of varchar when th= e data is a JAVA-String? Is anyone using LO and the LO-api for storing larg= e character-data? I think not. The @Lob annotation (which started this thre= ad) is just there to help other DBs map the String-property correctly, I wo= uld love to get rid of it but need it to have my app work with Oracle. I al= so want other properties of varchar to work (LIKE-operator f.ex., which Ora= cle also support on CLOBs), and I don't know what the PG-LO type supports. = Honestly; I don't see any reason to use LOs at all, neither for BLOB (where= I use bytea) or CLOBs (where I use varchar). I get around this problem in Hibernate by using this combination: @Column(name=3D"my_name") @Lob @Type(type=3D"org.hibernate.type.StringClobType") ...but the StringClobType is deprecated. -- Andreas Joseph Krogh Senior Software Developer / CTO Public key: http://home.officenet.no/~andreak/public_key.asc ------------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Rosenholmveien 25 | know how to do a thing and to watch | 1414 Troll=C3=A5sen | somebody else doing it wrong, without | NORWAY | comment. | Org.nr: NO 981 479 076 | | | | Tlf: +47 24 15 38 90 | | Fax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | ------------------------+---------------------------------------------+