BAEL-5404 - UUID as Entity ID in MongoDB

Commit to spring-data-mongodb-2
This commit is contained in:
HARDEMAN Sebastien
2022-06-27 15:32:38 +02:00
parent 02e904b225
commit 073a07f4ca
12 changed files with 456 additions and 0 deletions
@@ -0,0 +1,23 @@
package com.baeldung.uuid.event;
import java.util.UUID;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import com.baeldung.uuid.model.UuidIdentifiedEntity;
public class UuidIdentifiedEntityEventListener extends AbstractMongoEventListener<UuidIdentifiedEntity> {
@Override
public void onBeforeConvert(BeforeConvertEvent<UuidIdentifiedEntity> event) {
super.onBeforeConvert(event);
UuidIdentifiedEntity entity = event.getSource();
if(entity.getId() == null) {
entity.setId(UUID.randomUUID());
}
}
}
@@ -0,0 +1,26 @@
package com.baeldung.uuid.model;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Book extends UuidIdentifiedEntity {
private String title;
private String author;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.uuid.model;
import java.util.UUID;
import org.springframework.data.annotation.Id;
public abstract class UuidIdentifiedEntity {
@Id
protected UUID id;
public UUID getId() {
return id;
}
public void setId(UUID id) {
if(this.id != null) {
throw new UnsupportedOperationException("ID is already defined");
}
this.id = id;
}
}
@@ -0,0 +1,11 @@
package com.baeldung.uuid.repository;
import java.util.UUID;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.uuid.model.Book;
public interface BookRepository extends MongoRepository<Book, UUID> {
}
@@ -0,0 +1,14 @@
package com.baeldung.uuid.repository;
import java.util.UUID;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import com.baeldung.uuid.model.UuidIdentifiedEntity;
@NoRepositoryBean
public interface CustomMongoRepository<T extends UuidIdentifiedEntity> extends MongoRepository<T, UUID> {
}
@@ -0,0 +1,52 @@
package com.baeldung.uuid.repository.impl;
import java.util.List;
import java.util.UUID;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import com.baeldung.uuid.model.UuidIdentifiedEntity;
import com.baeldung.uuid.repository.CustomMongoRepository;
public class CustomMongoRepositoryImpl<T extends UuidIdentifiedEntity> extends SimpleMongoRepository<T, UUID> implements CustomMongoRepository<T> {
public CustomMongoRepositoryImpl(MongoEntityInformation<T, UUID> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
}
@Override
public <S extends T> S save(S entity) {
generateId(entity);
return super.save(entity);
}
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {
entities.forEach(entity -> generateId(entity));
return super.saveAll(entities);
}
@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) {
entities.forEach(entity -> generateId(entity));
return super.insert(entities);
}
protected <S extends T> void generateId(S entity) {
if(entity != null && entity.getId() == null) {
entity.setId(UUID.randomUUID());
}
}
}