在我的 Web 应用程序中,我有一个文本区域,其用户填充的内容最终通过 Hibernate 持久化到数据库中。我一直遇到一个问题,当用户输入超过一定长度时,持久化失败。有没有办法通过 Hibernate Annotations 或在配置中指示此特定字段应支持更长的字符串,并且数据库列类型应反射(reflect)这一点?
这是我得到的异常(exception):
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
最佳答案
您可以在注释上使用长度参数,如下所示:
@Column(length=1000)
或者,如果您的数据库支持,您可以将列类型更改为类似文本的内容,如下所示:
@Column(columnDefinition="text")
如果您使用 hbm2ddl 更新,则将创建该列以使用该类型(特定于数据库)。
关于hibernate - 使用 Hibernate 问题持久化长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2112667/