问题描述
在我的 Web 应用程序中,我有一个文本区域,其用户填充的内容最终通过 Hibernate 持久化到数据库中.我一直遇到一个问题,当用户输入超过一定长度时,持久化失败.有没有办法通过 Hibernate Annotations 或在配置中指示此特定字段应支持更长的字符串,并且数据库列类型应反映这一点?
In my web application, I have a text area whose user-filled contents are ultimately persisted to the db with Hibernate. I have been running into an issue that when the user input is beyond a certain length, the persistence fails. Is there a way to indicate through Hibernate Annotations or in the configuration that this particular field should support longer strings, and that the database column type should reflect this?
这是我得到的例外:
Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'introText' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2007)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1443)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 41 more
推荐答案
您可以在注释上使用长度参数,如下所示:
You could use the length parameter on the annotation, like so:
@Column(length=1000)
或者,如果您的数据库支持,您可以将列类型更改为类似文本的内容,如下所示:
or you could change the column type to something like text if your database supports it, like so:
@Column(columnDefinition="text")
如果您使用 hbm2ddl 更新,则将创建该列以使用该类型(特定于数据库).
If you are using hbm2ddl update, and the column will be created to use that type instead (database specific).
这篇关于使用 Hibernate 问题持久化长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!