微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

String LOBs on PostgreSQL with Hibernate 3.6

For String properties that may contain more than 255 characters,it is advised to add a @Lob annotation to the appropriate property. For example,to keep a comment text in an entity,one would write:

@Lob
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }

On Postgresql,a large string is usually kept in a TEXT column (instead of VARCHAR). However,after updating to Hibernate 3.6,an exception was suddenly thrown when accessing such a property (along with an sqlState: 22003 from Postgresql):

org.postgresql.util.PsqlException: Bad value for type long : This is some text...
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toLong(AbstractJdbc2ResultSet.java:2796)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2019)
    at org.postgresql.jdbc4.Jdbc4ResultSet.getClob(Jdbc4ResultSet.java:43)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getClob(AbstractJdbc2ResultSet.java:384)
        ... and more

ObvIoUsly,the Postgresql connector tried to interprete the textual content as a long integer Now,which – of course – results in a failure. Postgresql kNows two ways of storing binary large objects,either as BYTEA within the table space,or as OID in a separate place and referenced by a numerical identifier. It seems that Hibernate 3.6 suddenly treats TEXT columns like OID columns as well.
In the internet,I have found similar problems related to @Lob annotated byte[] properties. A common solution was to add a @Type annotation to the property.
An attempt to add @Type(type = “org.hibernate.type.MaterializedClobType”) did not help at all. Instead,@Type(type = “org.hibernate.type.TextType”) did the trick:

@Lob
@Type(type = "org.hibernate.type.TextType")
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }
Update: An alternative proposed by valSaraj in the comments is to use @Column(columnDeFinition = “text”),which requires not to use @Lob at Strings at all:
@Column(columnDeFinition = "text")
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }

I think this is the better solution,however it requires the underlying database to support the text column type. For Postgresql,it Now works fine again,even without needing to alter the table column type. However I Couldn’t check the impact of the annotation on other DBMS (like Oracle). Your Feedback is welcome.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐