Issue persisting long strings with Hibernate
Asked Answered
F

4

14

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
Fao answered 21/1, 2010 at 20:35 Comment(4)
why do you say it fails? does it truncate the string silently on the backend or do you get an error message? If you tell hibernate that it is string type it will try to save everything.Adhesive
The user content is set in a String object. An exception is thrown during persistence.Fao
could you put the exception in your post? :) and perhaps the snippet from your current mapping.Adhesive
Arthur, thanks for following up. I will post the exception as soon as I can.Fao
M
29

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

Myriagram answered 21/1, 2010 at 21:6 Comment(4)
just to complement, here the length needed to be an int, and therefore @Column(length=1000)Calamite
For our project we like to have the psql-database use the VARCHAR type for some columns without a specified size. For this we use: @Column(columnDefinition="VARCHAR")Clan
Especially since in PostgreSQL "varchar" is the same as "text". #4849464Ganister
I tried this but getting error. help please #25094910Jovia
E
6

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

@Type(type="text")
End answered 5/7, 2013 at 17:33 Comment(0)
S
4

For long strings or other large object types there is a special @Lob annotation that should solve your persistence problems, especially if you let Hibernate generate your database schema automatically. Therefore your entity code will look like this:

import javax.persistence.Lob;
...
@Lob
private String introText;

I've checked out the automatic schema generation with Hibernate and @Lob annotated String and in MariaDB I've got a column of type LONGTEXT which has a maximum length of 4,294,967,295 characters (~4 GB of data).


Be aware that if your application.properties has the following Hibernate configuration spring.jpa.hibernate.ddl-auto=update, then Hibernate does not automatically change your current column type to LONGTEXT. You will therefore have to update the column type either manually with SQL tools of your choice or (not safe solution, to use only if you work with temporary test data and could easily restore it) drop the database and let Hibernate recreate it from scratch.
Sempiternal answered 29/1, 2020 at 20:32 Comment(0)
A
0

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.

Adhesive answered 24/1, 2010 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.