Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Incorrect string value" when inserting UUID into MySQL database

I have the following data class:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "task")
public class Task {

    @Id
    @Column(name = "id", nullable = false, updatable = false)
    private UUID id;  // = UUID.randomUUID()

    @Column(name = "account_id", nullable = false)
    private Long accountId;

    ...
}

I am trying to save this object to our MySQL 5.7 database. However I am getting the following error:

    insert 
    into
        `
        task` (
            `account_id`, `created_at`, `task_status`, `task_type`, `id`
        ) 
    values
        (?, ?, ?, ?, ?)
2022-08-15 08:57:57.943 TRACE 588 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2022-08-15 08:57:57.943 TRACE 588 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [TIMESTAMP] - [2022-08-15T08:57:57.943419Z]
2022-08-15 08:57:57.943 TRACE 588 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [RUNNING]
2022-08-15 08:57:57.943 TRACE 588 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [USER_IMPORT]
2022-08-15 08:57:57.944 TRACE 588 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [BINARY] - [c1808d19-f448-4d4f-9908-9e767d56b04f]
2022-08-15 08:57:57.945 ERROR 588 --- [           main] o.h.e.jdbc.batch.internal.BatchingBatch  : HHH000315: Exception executing batch [java.sql.BatchUpdateException: Incorrect string value: '\xC1\x80\x8D\x19\xF4H...' for column 'id' at row 1], SQL: insert into `task` (`account_id`, `created_at`, `task_status`, `task_type`, `id`) values (?, ?, ?, ?, ?)
2022-08-15 08:57:57.945  WARN 588 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1366, SQLState: HY000
2022-08-15 08:57:57.945 ERROR 588 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Incorrect string value: '\xC1\x80\x8D\x19\xF4H...' for column 'id' at row 1
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.04 sec <<< FAILURE! - in com.validity.entitlementui.service.TaskServiceIntegrationTest
saveTaskTest(com.validity.entitlementui.service.TaskServiceIntegrationTest)  Time elapsed: 0.036 sec  <<< ERROR!
org.springframework.orm.jpa.JpaSystemException: could not execute batch; nested exception is org.hibernate.exception.GenericJDBCException: could not execute batch
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)

Every other question I found about this error had issues with emoji or non-Latin characters but as you can see I'm just trying to insert a v4 UUID.

I am defining the id column as VARCHAR(36) and in the database options am setting the character set to utf8mb4 (if that matters). Any ideas what's going on here?

like image 230
Matt Avatar asked Jun 18 '26 09:06

Matt


1 Answers

If you want to use a string/varchar representation in the table for the UUID column, with Hibernate 6 you can use:

@Id
@GeneratedValue
@JdbcTypeCode(SqlTypes.VARCHAR)
private UUID id;

Alternatively, as @Akina mentioned in his comment, changing the table column type to VARBINARY(36) also works, without needing the @JdbcTypeCode annotation.

like image 200
Mihai Ocneanu Avatar answered Jun 19 '26 23:06

Mihai Ocneanu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!