How to fix "class UUID cannot be cast to class String"

1.2k Views Asked by At

I'm storing a UUID in Mongo DB and when I want to read it again from MongoDB with

UUID.fromString(document.getString("userId"))

I get:

class java.util.UUID cannot be cast to class java.lang.String (java.util.UUID and java.lang.String are in module java.base of loader 'bootstrap')

java.lang.ClassCastException: class java.util.UUID cannot be cast to class java.lang.String 

But the Document class of the Java Mongo Driver has no .getUUID() method. How to fix this?

I was expecting to be able to read the UUID without errors.

1

There are 1 best solutions below

0
hb0 On

The fix seems to be:

(UUID)document.get("userId")