DEV Community

loading...

JPA, MySQL and UUIDs are not friends, apparently...

kudzaitsapo profile image Kudzai Tsapo ・1 min read

Today I was being myself as usual, creating an API with a UUID as a primary key. All was well, until JPA decided it wasn't. 😑

I had created a OneToOne relationship between one entity, and my User entity ( which had a UUID as Id ). In my defense ( before you start judging ) everything was working well. However, I started getting inconsistencies.

I had a method for retrieving paginated entities, and this method started throwing some EntityNotFound exception! After some hours of googling what was the problem, I found a work around, which was putting a @NotFound on the relationship. This is ... eh ... not quite a good idea, so I decided to screw it.

That's when I discovered the problem was that JPA could handle UUIDs, but MySQL had issues. Apparently, mysql stored UUIDs as binary, and JPA was trying to search using the UUID as a string, and that wasn't working.

What I didn't get though, was why on one method it was working perfectly, but on another, being inconsistent. If there's anyone who can explain to me WTF caused this issue, please feel free to say something.

Discussion (1)

Collapse
aleksey profile image
Aleksey Stukalov

Paste @Type(type="uuid-char") over your id field - this should help :)!

Forem Open with the Forem app