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?