将主键更新为UUID的记录在Spring Boot中不起作用 [英] Updating a record with primary key as UUID not working in Spring Boot

查看:466
本文介绍了将主键更新为UUID的记录在Spring Boot中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot v1.5.3.RELEASE和MYSQL作为后端来创建Restful服务.

I am using Spring Boot v1.5.3.RELEASE and MYSQL as Backend for creating Restful service.

我有一个TransactionTbl表,并希望它的主键是uuid类型,因为我认为该表中将有很多记录.

I have a TransactionTbl table and wanted its primary key to be a uuid type as I think there will be lots of records in that table.

实体中的主键定义:

@Id
@GenericGenerator(name = "uuid",strategy = "org.hibernate.id.UUIDGenerator",
    parameters={ @Parameter (name = "uuid_gen_strategy_class",
    value = "org.hibernate.id.uuid.CustomVersionOneStrategy") })
@GeneratedValue(generator =  "uuid")
@Column(name="txn_id")
private UUID txnId;

要更新的代码:

 txnObj = txnService.findOne(txnObj.getTxnId());
 txnObj.setAmt(someUpdatedAmountValue);
 txnService.save(txnObj);

上面的代码块运行时,抛出org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1错误

When the above code block runs it throws a org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 error

当我尝试使用@Id @GeneratedValue(strategy = GenerationType.AUTO)将主键从UUID切换到Long时,它按预期方式工作.

When I tried switching the primary key from UUID to a Long with @Id @GeneratedValue(strategy = GenerationType.AUTO) it is working as expected.

我是否以错误的方式定义了UUID,或者如果我们使用UUID生成,这样的更新将无法正常工作?

Am I defining the UUID in a wrong way or updates like this will not work if we use UUID generation?

推荐答案

**@Type(type = "uuid-char")**
@Column(name="txn_id")
private UUID txnId;

常规UUID生成在数据库中创建类型为Binary的列. 如上所示指定类型,同时生成UUID将解决此问题,因为它将列类型更改为Varchar 添加类型注释后

Normal UUID generation creates the column in database with type Binary. Specifying the Type as shown above, while generating the UUID will solve the problem, as it will change the Column type to Varchar After adding the Type Annotation

这篇关于将主键更新为UUID的记录在Spring Boot中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆