[英]Hibernate uuid generation gets 'data too long' error
我在使用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)
使用@Column(name="REQUEST_ID", length=32)
,當前正在生成的變量char長度大於32。您也可以使用@Length(max=32)
更新:作為@ cliff.meyers的答案, uuid2確實根據此休眠文檔在生成的字符串中使用了四個' -'uuid2使用org.hibernate.id.uuid.StandardRandomStrategy
作為默認的生成策略,該策略使用
通過java.util.UUID的randomUUID方法獲得“版本3”(又稱“隨機”)UUID值
該參考文獻將其指定為36個字符長。 您需要將REQUEST_ID
更改為varchar(36)
或使用strategy = "uuid"
,它是string of 32 hexidecimal digits
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.