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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
I would do this by implementing an onBeforeSave
listener, or possibly an onBeforeConvert
listener, as detailed in the documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With