将主键作为 UUID 的记录更新在 Spring 引导中不起作用



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

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

实体中的主键定义:

@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错误

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

我是否以错误的方式定义 UUID,或者如果我们使用 UUID 生成,这样的更新将不起作用?

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

正常的 UUID 生成会在数据库中创建类型为二进制的列。在生成 UUID 时指定如上所示的类型将解决问题,因为它会将列类型更改为 Varchar添加类型注释后

最新更新