Wrong save or read unicode value on @Lob String field in PostgreSQL 8.4

Description

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbcostgresql://localhost/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

@Entity
public class ReportTemplate{

@Id
@GeneratedValue
private Integer id;

@Lob
private String body;
...
}

At save (or at read) operation with 'body' field value is broken. In DB stored Blob's OID.

The next code throw AssertionError:

String str="Русский текст";//Unicode string
rt = new ReportTemplate();
rt.setBody(str);
Serializable id = hsm.getSession().save(rt);
hsm.commit();
hsm.getSession().clear();
rt = (ReportTemplate) hsm.getSession().get(ReportTemplate.class, id);
assert str.equals(rt.getBody());

Attachments

1

Activity

Brett MeyerSeptember 27, 2013 at 5:08 AM

Rejecting based on Lukasz's findings – thanks Lukasz!

Lukasz AntoniakMay 2, 2013 at 7:30 AM

Strange. I have tested current maser branch, and save operation did not work correctly. Database created with: CREATE DATABASE "hibtest" WITH OWNER "lukasz" ENCODING 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';.

In my opinion PostgreSQL JDBC driver causes hereby defect. Refer to linked bug report #5637: "JDBC driver method setClob() always uses getAsciiStream()".

Two possible workarounds for Hibernate users:

  1. Annotate @Lob property with @Type(type="org.hibernate.type.StringClobType"). Note that org.hibernate.type.StringClobType is deprecated and new org.hibernate.type.MaterializedClobType does not fix reported case.

  2. Provide custom PostgreSQL dialect as the one attached to JIRA ticket. By now I did not observe any disadvantages of using PreparedStatement#setCharacterStream() method in comparison to PreparedStatement#setClob(). and should have a look. Moreover, after executing some basic load tests (persist CLOB followed by a single read), it seems that stream operations perform better.

Analogical issue rejected by Spring: https://jira.springsource.org/browse/SPR-7520.

Did my suggestions work for you?

Vladimir LoshchinMarch 26, 2013 at 7:53 AM

Is there are any activity by this issue?
I repropuce this issue on Hibernate 4.1.9. (Befor update, on 3.4.0.GA worked fine)
Problem in hibernate read procedure. Lob looks fine in table column by PGAdmin.

Won't Fix

Details

Assignee

Reporter

Affects versions

Priority

Created April 16, 2011 at 1:08 PM
Updated September 27, 2013 at 5:08 AM
Resolved September 27, 2013 at 5:08 AM