Issue persisting long strings with Hibernate

a 夏天 提交于 2019-12-08 17:43:46

问题


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?

Here's the exception that I'm getting:

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

回答1:


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")

If you are using hbm2ddl update, and the column will be created to use that type instead (database specific).




回答2:


I had a similar issue that I solved by assigning the hibernate "text" type to the property:

@Type(type="text")



回答3:


ok that is a DB error actually.

Data too long for column 'introText'

check the introText column in your DB and it is probably a varchar that is just limited in size. You will need to change the storage type to something larger so it won't truncate your text.

If you think that isn't it you will have to show your mapping and schema.



来源:https://stackoverflow.com/questions/2112667/issue-persisting-long-strings-with-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!