Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java spring data mongodb: UUID as entity ID in mongodb

I'm building spring boot application with spring-boot-starter-data-mongodb:

I have an etity which looks like:

@Document(collection = "nodes")
@Data
@Builder
public class Node {

    @Id
    UUID id;

    String info;
}

and repository interface:

public interface NodesRepository extends MongoRepository<Node, UUID> {
}

The problem is that when I insert Node object with empty id field application throws exception with the following message:

Cannot autogenerate id of type java.util.UUID for entity of type model.Node!

I know that out of box spring mongodb supports String and BigInteger id types for autogeneration.

So I want to know is it possible to use UUID as type for my id field or not? Can I create some type of custom MappingMongoConverter for my entity?

like image 296
Sergei Podlipaev Avatar asked Feb 28 '19 11:02

Sergei Podlipaev


People also ask

Are MongoDB IDs UUID?

Overview. By default, the MongoDB Java driver generates IDs of the type ObjectId. Sometimes, we may want to use another type of data as the unique identifier of an object, such as a UUID. However, the MongoDB Java driver can't generate UUIDs automatically.

How do you represent UUID in java?

Java UUID Representation The representation of the UUID uses hex digits. Java UUID is made up of hex digit along with four hyphens (-). It is 36 characters long unique number, including four hyphens. A UUID may be nil, in which all bits are set to zero.

What is the datatype of UUID in java?

Class UUID. A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers.

What is MongoDB UUID?

A MongoDB ObjectID is 12 bytes in size, is packed for storage, and its parts are organized for performance (i.e. timestamp is stored first, which is a logical ordering criteria). Conversely, a standard UUID is 36 bytes, contains dashes and is typically stored as a string.

Which MongoDB ID types can be used with spring data?

When using Spring Data MongoDB IDs can be automatically generated for documents provided that you’re using an ObjectId, String or BigInteger as the underlying type. What if you would like to use some other, non-autogeneratable type for IDs? Under normal circumstances the MongoDB driver generates a unique ID for objects to be persisted.

Does MongoDB generate UUIDs automatically?

Overview By default, the MongoDB Java driver generates IDs of the type ObjectId. Sometimes, we may want to use another type of data as the unique identifier of an object, such as a UUID. However, the MongoDB Java driver can't generate UUIDs automatically.

What is GUID in MongoDB?

A GUID is one of many implementations of the UUID standard. UUIDs are 128-bit values and are usually displayed as 32 hexadecimal digits separated by hyphens, for example: MongoDB has built-in support for the UUID data type and most of the MongoDB drivers support UUID natively.

How to integrate MongoDB with Spring Boot web MVC?

Configuration for Spring Data MongoDB is in application.properties. pom.xml contains dependencies for Spring Boot Web MVC and Spring Data MongoDB. Let’s implement this application right now. Use Spring web tool or your development tool ( Spring Tool Suite, Eclipse, Intellij) to create a Spring Boot project.


2 Answers

You can solve this problem by creating a CustomRepository

@NoRepositoryBean
public interface CustomMongoRepository<T extends BaseEntity>
    extends MongoRepository<T, Long> {
}

public class CustomMongoRepositoryImpl<T extends BaseEntity>
        extends SimpleMongoRepository<T, Long> implements CustomMongoRepository<T> {

    CustomMongoRepositoryImpl(
        MongoEntityInformation<T, Long> entityInformation,
        MongoOperations mongoOperations) {

        super(entityInformation, mongoOperations);
    }

    @Override
    public <S extends T> S insert(S entity) {
        generateId(entity);
        return super.insert(entity);
    }

    @Override
    public <S extends T> List<S> insert(Iterable<S> entities) { ... }

    @Override
    public <S extends T> S save(S entity) { ... }

    @Override
    public <S extends T> List<S> save(Iterable<S> entities) { ... }

    protected <S extends T> void generateId(S entity) { ... }

}

@SpringBootApplication
@EnableMongoRepositories(repositoryBaseClass = CustomMongoRepositoryImpl.class)
public class Application {
    ...
}

I hope this is helpful.

like image 62
Nilanjan Bhadury Avatar answered Oct 06 '22 23:10

Nilanjan Bhadury


I would do this by implementing an onBeforeSave listener, or possibly an onBeforeConvert listener, as detailed in the documentation here.

like image 30
Mark B Avatar answered Oct 06 '22 23:10

Mark B