Vishal Nair
Vishal Nair

Reputation: 2191

Updating a record with primary key as UUID not working in Spring Boot

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

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.

Primary key definition in the Entity:

@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;

Code to Update :

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

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

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

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

Upvotes: 1

Views: 4413

Answers (1)

Niravra Kar
Niravra Kar

Reputation: 56

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

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

Upvotes: 4

Related Questions