我在使用 hibernate 持久化到 mysql 的表中时遇到以下错误:
WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] SQL Error: 1406, SQLState: 22001
ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] Data truncation: Data too long for column 'REQUEST_ID' at row 1
ERROR [some.class.ClassImpl] persist failed: org.hibernate.exception.DataException: Data truncation: Data too long for column 'REQUEST_ID' at row 1
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:71) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
这是我如何使用生成的值 uuid 作为我的主键
@Id
@Column(name="REQUEST_ID")
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
这是我在 mysql
数据库中的专栏:
+---------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+-------+
| REQUEST_ID | varchar(32) | NO | PRI | NULL | |
+---------------------+---------------+------+-----+---------+-------+
最佳答案
我很确定“uuid2”会生成一个带有破折号的 UUID,例如
4d8bef3d-f46f-4f4e-8cad-673feaed896c
尝试将您的数据库列设置为:
varchar(36)
并可选择将您的注释更新为以下内容:
@Column(name="REQUEST_ID", length=36)
@Length(min=36, max=36)
关于Hibernate uuid 生成出现 'data too long' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902467/