MySQL 8 with Spring Boot throws data truncation error when inserting 300 chars into longtext field

I’m having trouble with my Spring Boot app when trying to save text data to MySQL. Here’s my setup:

Environment:

  • MySQL version 8
  • Spring Boot 2.6.6
  • MySQL connector:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

The Problem:
I have a database field called “feedback” that I originally set as varchar(15000) with utf8mb4_0900_ai_ci collation. When I try to insert more than 255 characters, I get this error:

2025-06-28 18:00:51.208  WARN 74004 --- [io-8087-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1406, SQLState: 22001
2025-06-28 18:00:51.209 ERROR 74004 --- [io-8087-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : Data truncation: Data too long for column 'feedback' at row 1

I even changed the field type to longtext but still get the same issue. The weird thing is that I can insert much longer text using database tools like Navicat directly.

My Entity:

@Entity
@Getter
@Setter
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@Audited
@Table(name = "user_feedback", schema = "myapp", catalog = "")
public class UserFeedback {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long feedbackId;
    private Integer userId;
    private Short categoryType;
    private String feedback;
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private Boolean isPublic;
    private Integer reviewedBy;
    private Boolean isApproved;
    private Double rating;
    
    @CreatedBy
    @Column(name = "created_by", nullable = false, updatable=false)
    private Integer createdBy;
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable=false)
    private LocalDateTime createdAt;
    @LastModifiedBy
    @Column(name = "updated_by")
    private Integer updatedBy;
    @LastModifiedDate
    @Column(name="updated_at")
    private LocalDateTime updatedAt;
    
    @Transient
    Page<FileAttachment> attachments;
    
    public UserFeedback() {}
}

Database Table:

CREATE TABLE `user_feedback` (
  `feedbackId` bigint NOT NULL AUTO_INCREMENT,
  `userId` int DEFAULT NULL,
  `categoryType` smallint DEFAULT '0',
  `feedback` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
  `isPublic` tinyint(1) DEFAULT '1',
  `createdBy` int DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updatedBy` int DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `reviewedBy` int DEFAULT NULL,
  `rating` double DEFAULT NULL,
  `isApproved` bit(1) DEFAULT b'0',
  PRIMARY KEY (`feedbackId`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

The issue seems to be with Hibernate or Spring Boot configuration rather than MySQL itself. Any ideas what could be causing this character limit?

Had this exact problem last year. Hibernate was treating the field as regular varchar even though the database schema showed longtext. You’ve got to explicitly tell your entity about the column type. Add @Column(columnDefinition = "LONGTEXT") above your feedback field in the UserFeedback entity. Hibernate defaults to varchar(255) no matter what your actual database column is unless you specify otherwise. Also double-check your hibernate config. Make sure spring.jpa.hibernate.ddl-auto is set right and spring.jpa.database-platform is configured for MySQL8. Your connector version’s fine, but the JPA layer needs explicit column type info.

This looks like a Hibernate schema mapping disconnect. I hit something similar with MySQL 8 where the ORM wasn’t picking up the actual column definition. Beyond adding @Column(columnDefinition = "LONGTEXT"), verify Hibernate’s using the right dialect. Check if you’ve got spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect explicitly set in your properties file. Spring Boot’s auto-detection sometimes picks the wrong dialect version. Also enable SQL logging with spring.jpa.show-sql=true to see what DDL Hibernate thinks it’s working with. Since direct database insertion works, your schema’s fine - this is definitely a JPA layer issue where Hibernate cached or assumed wrong column metadata.

Hit this exact issue during a migration a few months ago. The missing @Column(columnDefinition = "LONGTEXT") annotation is part of it, but MySQL 8’s strict mode is probably the real culprit here. Even with the right annotation, you’ll still get truncation errors if your connection string doesn’t handle MySQL 8’s stricter validation. Add sessionVariables=sql_mode='TRADITIONAL' or sessionVariables=sql_mode='' to your database URL in application.properties. MySQL 8 turns on strict mode by default, which causes data truncation even when your column can handle the data size. Also make sure you’re using org.hibernate.dialect.MySQL8Dialect instead of the generic MySQL dialect. Fix both the column definition and SQL mode - that should kill the truncation error.

Check your application.properties connection string - MySQL 8 has weird defaults that cause this. Add allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false to your datasource URL. Also, Hibernate might be caching old schema info, so restart your app after adding the @Column annotation others mentioned. I had the same issue and it was the connection params, not the entity setup.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.