在使用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 | |
+---------------------+---------------+------+-----+---------+-------+发布于 2014-02-20 14:02:24
我很确定"uuid2“会生成一个包含破折号的UUID。
4d8bef3d-f46f-4f4e-8cad-673feaed896c
尝试将数据库列设置为:
varchar(36)
并可选择地将注释更新为以下内容:
@Column(name="REQUEST_ID", length=36)
@Length(min=36, max=36)
发布于 2014-02-20 09:09:28
使用@Column(name="REQUEST_ID", length=32),当前正在生成的变量char长度大于32。您也可以使用@Length(max=32)
更新: as @cliff.meyers uuid2确实在生成的字符串中使用四'-‘根据这 hibernate文档uuid2使用org.hibernate.id.uuid.StandardRandomStrategy作为默认生成策略
"version 3“(又名”随机“)通过randomUUID方法java.util.UUID的UUID值
它由这引用指定为36个字符长。您需要将REQUEST_ID更改为varchar(36)或使用strategy = "uuid",即string of 32 hexidecimal digits。
https://stackoverflow.com/questions/21902467
复制相似问题