0

I'm trying to insert an HTML blob into our sql-server2005 database. I've been using the data-type [text] for the field the blob will eventually live in. I've also put a '@Lob' annotation on the field in the domain model. The problem comes in when the HTML blob I'm attempting to store is larger than 65536 characters.

It seems that is the character-limit for a text data type when using the @Lob annotation. Ideally I'd like to keep the whole blob intact rather than chunk it up into multiple rows in the database.

Allow me to clarify annotation:

 @Lob   
 @Column(length = Integer. MAX_VALUE) //per an answer on stackoverflow  
 private String htmlBlob;  

database side (sql-server-2005):

CREATE TABLE dbo.IndustrySectorTearSheetBlob(  
   ...  
      htmlBlob text NULL    
...
)

Still seeing truncation after 65536 characters...

EDIT: I've printed out the contents of all possible strings (only 10 right now) that would be inserted into the Database. Each string seems to contain all characters, judging by the fact that the close html tag is present at the end of the string....

6
  • show your mappings / annotations
    – Bozho
    Dec 7, 2009 at 15:56
  • HTML BLOB? What the hell is that? Why would you use a BLOB for characters? Dec 7, 2009 at 16:00
  • it seems like CLOB, but it's called 'text' is MS SQL. So "HTML Lob" would be more correct.
    – Bozho
    Dec 7, 2009 at 16:01
  • 1
    Just for the record, TEXT, NTEXT and IMAGE are "deprecated" since SQL Server 2005 and should be avoided in favor of VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX). Dec 7, 2009 at 16:37
  • does my latest edit clarify the mappings enough or did i forget something?
    – Ramy
    Dec 7, 2009 at 20:06

3 Answers 3

4

You could look at annotating with this also

@Column(length = Integer.MAX_VALUE)

Not sure why a blob is necessary though, NVARCHAR(MAX) will store all the html you want.

1

Actually, I think that what you're looking for is a CLOB field. Quoting Using Advanced Data Types:

BLOB and CLOB and NCLOB Data Types

The JDBC driver implements all the methods of the java.sql.Blob, java.sql.Clob, and java.sql.NClob interfaces.

Note: CLOB values can be used with SQL Server 2005 large-value data types. Specifically, CLOB types can be used with the varchar(max) and nvarchar(max) data types, BLOB types can be used with varbinary(max) and image data types, and NCLOB types can be used with ntext and nvarchar(max).

In other words, use a VARCHAR(MAX) or a NVARCHAR(MAX) if you need unicode support. About their maximum length:

The maximum storage size for VARCHAR(MAX) is 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. Since each character in a VARCHAR data type uses one byte, the maximum length for a VARCHAR(MAX) data type is 2,147,483,645.

The maximum storage size for NVARCHAR(MAX) is also 2^31-1 bytes (2,147,483,647 bytes or 2GB - 1 bytes). The storage size, in bytes, is two times the number of characters entered + 2 bytes. The data entered can be 0 characters in length. Since each Unicode character in an NVARCHAR data type uses two bytes, the maximum length for an NVARCHAR(MAX) data type is 1,073,741,822.

That should be enough for your HTML.

EDIT: On the Hibernate side, your annotated entity looks fine. On the database side, it should be ok. However, could you try to use VARCHAR(MAX) instead of TEXT (and remove this doubt about TEXT).

CREATE TABLE dbo.IndustrySectorTearSheetBlob (
...
htmlBlob varchar(max) NULL
... 
)

By the way, what Hibernate dialect are you using? And what JDBC driver are you using?

9
  • i've tried varchar(max), nvarchar(max) with the same truncation results. When I use varbinary(max), I get the following message: "com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion from data type nvarchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query." not sure what Hibernate dialect I'm using. I think JDBC driver is:<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class> (taken from a JBoss config file)
    – Ramy
    Dec 7, 2009 at 21:05
  • Actually, you don't want to use varbinary(max) for characters (hence the exception). Then, there may be an issue with nvarchar(max) (see opensource.atlassian.com/projects/hibernate/browse/HHH-3405). That's why I was suggesting to use varchar(max). Sadly, this doesn't change anything (I would have been suprised though). Regarding the dialect, it would be nice if you could confirm that you are using org.hibernate.dialect.SQLServerDialect. And about the JDBC driver, I didn't find anything about such a limitation. Could you try straight JDBC? Dec 7, 2009 at 21:27
  • Pascal, I'm not sure how to confirm this. I don't find anything in the workspace when i search for the string "org.hibernate.dialect" in any java file.
    – Ramy
    Dec 7, 2009 at 21:41
  • Also, i'm not sure what you mean by "try straight JDBC"
    – Ramy
    Dec 7, 2009 at 21:44
  • 1. The dialect should be available in your hibernate.cfg.xml or hibernate.properties. 2. By straight JDBC, I mean without using Hibernate, just the JDBC API. Dec 7, 2009 at 21:53
0

Hmm, I haven't used Hibernate with Sql-Server 2005, but I have used Oracle TopLink with MySQL. I in my object I used used a straight byte[] with no annotations at all and it worked fine.

So, if I were you I would try storing your HTML in an encoded byte[] (use UTF-8 or whatever). Whenever you need to access the HTML text as a string, you can just decode it.

Theoretically, your text shouldn't be getting cut off, but sometimes these things fall short.

1
  • That would be unnecessary. A String Lob should work just fine, if mapped properly.
    – Bozho
    Dec 7, 2009 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.