BAEL-5404 - UUID as Entity ID in MongoDB
Initial commit
This commit is contained in:
		
							parent
							
								
									f031b68721
								
							
						
					
					
						commit
						f77b93b7a4
					
				| @ -0,0 +1,32 @@ | ||||
| package com.baeldung.config; | ||||
| 
 | ||||
| import org.bson.UuidRepresentation; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||||
| import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||||
| 
 | ||||
| import com.baeldung.repository.impl.CustomMongoRepositoryImpl; | ||||
| import com.mongodb.ConnectionString; | ||||
| import com.mongodb.MongoClientSettings; | ||||
| import com.mongodb.client.MongoClient; | ||||
| import com.mongodb.client.MongoClients; | ||||
| 
 | ||||
| @Configuration | ||||
| @EnableMongoRepositories(basePackages = "com.baeldung.repository", repositoryBaseClass = CustomMongoRepositoryImpl.class) | ||||
| public class CustomRepositoryMongoConfig { | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoClient mongo() throws Exception { | ||||
|         final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); | ||||
|         final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() | ||||
|                                                                     .uuidRepresentation(UuidRepresentation.STANDARD) | ||||
|                                                                     .applyConnectionString(connectionString).build(); | ||||
|         return MongoClients.create(mongoClientSettings); | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoTemplate mongoTemplate() throws Exception { | ||||
|         return new MongoTemplate(mongo(), "test"); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,48 @@ | ||||
| package com.baeldung.config; | ||||
| 
 | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import org.bson.UuidRepresentation; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||||
| import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback; | ||||
| import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||||
| 
 | ||||
| import com.baeldung.model.UuidIdentifiedEntity; | ||||
| import com.mongodb.ConnectionString; | ||||
| import com.mongodb.MongoClientSettings; | ||||
| import com.mongodb.client.MongoClient; | ||||
| import com.mongodb.client.MongoClients; | ||||
| 
 | ||||
| @Configuration | ||||
| @EnableMongoRepositories(basePackages = "com.baeldung.repository") | ||||
| public class EntityCallbackMongoConfig { | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoClient mongo() throws Exception { | ||||
|         final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); | ||||
|         final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() | ||||
|                                                                     .uuidRepresentation(UuidRepresentation.STANDARD) | ||||
|                                                                     .applyConnectionString(connectionString).build(); | ||||
|         return MongoClients.create(mongoClientSettings); | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoTemplate mongoTemplate() throws Exception { | ||||
|         return new MongoTemplate(mongo(), "test"); | ||||
|     } | ||||
|      | ||||
|     @Bean | ||||
|     public BeforeConvertCallback<UuidIdentifiedEntity> beforeSaveCallback() { | ||||
|          | ||||
|         return (entity, collection) -> { | ||||
|            | ||||
|             if(entity.getId() == null) { | ||||
|                 entity.setId(UUID.randomUUID()); | ||||
|             } | ||||
|             return entity; | ||||
|         };         | ||||
|     } | ||||
|      | ||||
| } | ||||
| @ -0,0 +1,38 @@ | ||||
| package com.baeldung.config; | ||||
| 
 | ||||
| import org.bson.UuidRepresentation; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||||
| import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||||
| 
 | ||||
| import com.baeldung.event.UuidIdentifiedEntityEventListener; | ||||
| import com.mongodb.ConnectionString; | ||||
| import com.mongodb.MongoClientSettings; | ||||
| import com.mongodb.client.MongoClient; | ||||
| import com.mongodb.client.MongoClients; | ||||
| 
 | ||||
| @Configuration | ||||
| @EnableMongoRepositories(basePackages = "com.baeldung.repository") | ||||
| public class EventMongoConfig { | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoClient mongo() throws Exception { | ||||
|         final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); | ||||
|         final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() | ||||
|                                                                     .uuidRepresentation(UuidRepresentation.STANDARD) | ||||
|                                                                     .applyConnectionString(connectionString).build(); | ||||
|         return MongoClients.create(mongoClientSettings); | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public MongoTemplate mongoTemplate() throws Exception { | ||||
|         return new MongoTemplate(mongo(), "test"); | ||||
|     } | ||||
|      | ||||
|     @Bean | ||||
|     public UuidIdentifiedEntityEventListener uuidIdentifiedEntityEventListener() { | ||||
|    | ||||
|         return new UuidIdentifiedEntityEventListener(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| package com.baeldung.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.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.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.model; | ||||
| 
 | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import org.springframework.data.annotation.Id; | ||||
| 
 | ||||
| public abstract class UuidIdentifiedEntity { | ||||
| 
 | ||||
|     @Id    | ||||
|     private 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.repository; | ||||
| 
 | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import org.springframework.data.mongodb.repository.MongoRepository; | ||||
| 
 | ||||
| import com.baeldung.model.Book; | ||||
| 
 | ||||
| public interface BookRepository extends MongoRepository<Book, UUID> { | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| package com.baeldung.repository; | ||||
| 
 | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import org.springframework.data.mongodb.repository.MongoRepository; | ||||
| import org.springframework.data.repository.NoRepositoryBean; | ||||
| 
 | ||||
| import com.baeldung.model.UuidIdentifiedEntity; | ||||
| 
 | ||||
| @NoRepositoryBean | ||||
| public interface CustomMongoRepository<T extends UuidIdentifiedEntity> extends MongoRepository<T, UUID> { | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,51 @@ | ||||
| package com.baeldung.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.model.UuidIdentifiedEntity; | ||||
| import com.baeldung.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()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,58 @@ | ||||
| package com.baeldung.repository; | ||||
| 
 | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| 
 | ||||
| import org.junit.After; | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.data.mongodb.core.MongoOperations; | ||||
| import org.springframework.data.mongodb.core.query.Criteria; | ||||
| import org.springframework.data.mongodb.core.query.Query; | ||||
| import org.springframework.test.context.ContextConfiguration; | ||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||
| 
 | ||||
| import com.baeldung.config.CustomRepositoryMongoConfig; | ||||
| import com.baeldung.model.Book; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * This test requires: | ||||
|  * * mongodb instance running on the environment | ||||
|  * | ||||
|  */ | ||||
| @RunWith(SpringJUnit4ClassRunner.class) | ||||
| @ContextConfiguration(classes = CustomRepositoryMongoConfig.class) | ||||
| public class CustomRepositoryLiveTest { | ||||
| 
 | ||||
|     @Autowired | ||||
|     private BookRepository bookRepository; | ||||
| 
 | ||||
|     @Autowired | ||||
|     private MongoOperations mongoOps; | ||||
| 
 | ||||
|     @Before | ||||
|     public void testSetup() { | ||||
|         if (!mongoOps.collectionExists(Book.class)) { | ||||
|             mongoOps.createCollection(Book.class); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @After | ||||
|     public void tearDown() { | ||||
|         mongoOps.dropCollection(Book.class); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenInsertingBook_thenBookIsInserted() { | ||||
|         final Book book = new Book(); | ||||
|         book.setTitle("The Lord of the Rings"); | ||||
|         book.setAuthor("JRR Tolkien"); | ||||
|         Book savedBook = bookRepository.save(book); | ||||
|                  | ||||
|         Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedBook.getId())), Book.class); | ||||
|                  | ||||
|         assertEquals(result.getTitle(), "The Lord of the Rings"); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,63 @@ | ||||
| package com.baeldung.repository; | ||||
| 
 | ||||
| import static org.junit.Assert.assertNotNull; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| 
 | ||||
| import org.junit.After; | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.data.mongodb.core.MongoOperations; | ||||
| import org.springframework.data.mongodb.core.query.Criteria; | ||||
| import org.springframework.data.mongodb.core.query.Query; | ||||
| import org.springframework.test.context.ContextConfiguration; | ||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||
| 
 | ||||
| import com.baeldung.config.EntityCallbackMongoConfig; | ||||
| import com.baeldung.model.Book; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * This test requires: | ||||
|  * * mongodb instance running on the environment | ||||
|  * | ||||
|  */ | ||||
| @RunWith(SpringJUnit4ClassRunner.class) | ||||
| @ContextConfiguration(classes = EntityCallbackMongoConfig.class) | ||||
| public class EntityCallbackLiveTest { | ||||
| 
 | ||||
|     @Autowired | ||||
|     private BookRepository bookRepository; | ||||
| 
 | ||||
|     @Autowired | ||||
|     private MongoOperations mongoOps; | ||||
| 
 | ||||
|     @Before | ||||
|     public void testSetup() { | ||||
|         if (!mongoOps.collectionExists(Book.class)) { | ||||
|             mongoOps.createCollection(Book.class); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @After | ||||
|     public void tearDown() { | ||||
|         mongoOps.dropCollection(Book.class); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSavingArticle_thenArticleIsInserted() { | ||||
|         final Book book = new Book(); | ||||
|         book.setTitle("The Lord of the Rings"); | ||||
|         book.setAuthor("JRR Tolkien"); | ||||
|          | ||||
|         Book savedArticle = bookRepository.save(book); | ||||
|                  | ||||
|         Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); | ||||
|          | ||||
|         assertNotNull(result); | ||||
|         assertEquals(result.getTitle(), "The Lord of the Rings"); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,63 @@ | ||||
| package com.baeldung.repository; | ||||
| 
 | ||||
| import static org.junit.Assert.assertNotNull; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| 
 | ||||
| import org.junit.After; | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.data.mongodb.core.MongoOperations; | ||||
| import org.springframework.data.mongodb.core.query.Criteria; | ||||
| import org.springframework.data.mongodb.core.query.Query; | ||||
| import org.springframework.test.context.ContextConfiguration; | ||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||
| 
 | ||||
| import com.baeldung.config.EventMongoConfig; | ||||
| import com.baeldung.model.Book; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * This test requires: | ||||
|  * * mongodb instance running on the environment | ||||
|  * | ||||
|  */ | ||||
| @RunWith(SpringJUnit4ClassRunner.class) | ||||
| @ContextConfiguration(classes = EventMongoConfig.class) | ||||
| public class EventLiveTest { | ||||
| 
 | ||||
|     @Autowired | ||||
|     private BookRepository bookRepository; | ||||
| 
 | ||||
|     @Autowired | ||||
|     private MongoOperations mongoOps; | ||||
| 
 | ||||
|     @Before | ||||
|     public void testSetup() { | ||||
|         if (!mongoOps.collectionExists(Book.class)) { | ||||
|             mongoOps.createCollection(Book.class); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @After | ||||
|     public void tearDown() { | ||||
|         mongoOps.dropCollection(Book.class); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSavingArticle_thenArticleIsInserted() { | ||||
|         final Book book = new Book(); | ||||
|         book.setTitle("The Lord of the Rings"); | ||||
|         book.setAuthor("JRR Tolkien"); | ||||
|          | ||||
|         Book savedArticle = bookRepository.save(book); | ||||
|                  | ||||
|         Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); | ||||
|          | ||||
|         assertNotNull(result); | ||||
|         assertEquals(result.getTitle(), "The Lord of the Rings"); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user