Mat*_*att 5 mysql hibernate jpa spring-boot
我有以下数据类:
@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;
...
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将此对象保存到我们的 MySQL 5.7 数据库中。但是我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
我发现的关于此错误的所有其他问题都与表情符号或非拉丁字符有关,但如您所见,我只是尝试插入 v4 UUID。
我将id列定义为VARCHAR(36),并在数据库选项中将字符集设置为utf8mb4(如果重要的话)。有什么想法吗?
如果您想在 UUID 列的表中使用 string/varchar 表示形式,则在 Hibernate 6 中您可以使用:
@Id
@GeneratedValue
@JdbcTypeCode(SqlTypes.VARCHAR)
private UUID id;
Run Code Online (Sandbox Code Playgroud)
或者,正如 @Akina 在他的评论中提到的,将表列类型更改为VARBINARY(36)也可以,无需@JdbcTypeCode注释。
| 归档时间: |
|
| 查看次数: |
1843 次 |
| 最近记录: |