Java/Hibernate/sql-server 2005 中的 Blob
我正在尝试将 HTML blob 插入到我们的 sql-server2005 数据库中。我一直使用数据类型 [text] 作为 blob 最终将驻留在其中的字段。我还在域模型中的字段上添加了“@Lob”注释。当我尝试存储的 HTML blob 大于 65536 个字符时,就会出现问题。
这似乎是使用 @Lob 注释时文本数据类型的字符限制。理想情况下,我希望保持整个 blob 完整,而不是将其分成数据库中的多行。
请允许我澄清一下 注释:
@Lob
@Column(length = Integer. MAX_VALUE) //per an answer on stackoverflow
private String htmlBlob;
数据库端(sql-server-2005):
CREATE TABLE dbo.IndustrySectorTearSheetBlob(
...
htmlBlob text NULL
...
)
在 65536 个字符后仍然看到截断...
编辑:我已经打印出了将插入数据库的所有可能字符串的内容(现在只有 10 个)。每个字符串似乎都包含所有字符,从关闭 html 标签出现在字符串末尾的事实来看......
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你也可以看看用这个注释虽然
不知道为什么一个blob是必要的,但NVARCHAR(MAX)将存储你想要的所有html。
You could look at annotating with this also
Not sure why a blob is necessary though, NVARCHAR(MAX) will store all the html you want.
实际上,我认为您正在寻找的是 CLOB 字段。引用使用高级数据类型:
换句话说,如果需要 unicode 支持,请使用 VARCHAR(MAX) 或 NVARCHAR(MAX)。关于它们的最大长度:
这对于您的 HTML 来说应该足够了。
编辑:在 Hibernate 方面,带注释的实体看起来不错。在数据库方面,应该没问题。但是,您可以尝试使用 VARCHAR(MAX) 代替 TEXT (并消除对 TEXT 的怀疑)。
顺便问一下,您使用的是哪种 Hibernate 方言?您使用什么 JDBC 驱动程序?
Actually, I think that what you're looking for is a CLOB field. Quoting Using Advanced Data Types:
In other words, use a VARCHAR(MAX) or a NVARCHAR(MAX) if you need unicode support. About their maximum length:
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).
By the way, what Hibernate dialect are you using? And what JDBC driver are you using?
嗯,我没有将 Hibernate 与 Sql-Server 2005 一起使用,但我将 Oracle TopLink 与 MySQL 一起使用。我在我的对象中使用了一个直接的
byte[]
,根本没有注释,而且效果很好。所以,如果我是你,我会尝试将 HTML 存储在编码的 byte[] 中(使用 UTF-8 或其他格式)。每当您需要以字符串形式访问 HTML 文本时,只需对其进行解码即可。
理论上,你的文本不应该被切断,但有时这些事情是不够的。
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.