Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Entity UUID is stored as legacy UUID in MongoDB

I'm dumping some data into my MongoDb and generate a UUID on the way. In the collection, this UUID field is stored as LUUID (legacy UUID - type 3) and I don't know how to avoid this, because I would want the format to be the standard UUID (type 4).

Entity:

@Document(collection = "sms")
public class SmsEntity {
    ...
    private UUID ubmMessageSid;  // <- this field gets stored as LUUID
    ...

    public static class Builder {
    ...
        private UUID ubmMessageSid;
    ...

        public Builder ubmMessageSid(UUID ubmMessageSid) {
            this.ubmMessageSid = ubmMessageSid;
            return this;
        }

        public SmsEntity build() {return new SmsEntity(this);}
    }
}

Repo:

@Repository
public interface SmsRepository extends CrudRepository<SmsEntity, String> {
}

Service storing this entity:

...
var ubmId = UUID.randomUUID();
var smsEntity = SmsEntity.builder()
    .ubmMessageSid(ubmId)
    ...
    .build();
repository.save(smsEntity);

Anything I have to annotate or configure to store the UUID as Binary/type4?

like image 833
EngJon Avatar asked Mar 03 '23 06:03

EngJon


1 Answers

In Spring Boot 2, for Spring MongoDB 3.x, you can set this using the autoconfiguration properties:

# Options: unspecified, standard, c_sharp_legacy, java_legacy, python_legacy
spring.data.mongodb.uuid-representation=standard
like image 188
tlogbon Avatar answered Mar 05 '23 20:03

tlogbon